Java下载文件程序问题

publicclassDownLoadServletextendsDispatchAction{/***Constructoroftheobject.*/publicDo... public class DownLoadServlet extends DispatchAction {

/**
* Constructor of the object.
*/
public DownLoadServlet() {
super();
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public ActionForward doPost(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fileName = request.getParameter("fileName");
System.out.print(fileName+"dddddddddddddddddddddd");
fileName = new String(fileName.getBytes("ISO-8859-1"),"GB2312");
String fileNames[] = fileName.split("/");
String lastFileName = fileNames[fileNames.length-1];
response.setCharacterEncoding("UTF-8");
lastFileName =URLEncoder.encode(lastFileName, "UTF-8");
System.out.println("lastFileName:"+lastFileName);
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("ISO-8859-1"),"UTF-8"));
InputStream inputStream = new FileInputStream(new File(fileName));
OutputStream outputStream = response.getOutputStream();
int i = 0;
byte b[] = new byte[1024];
while ((i=inputStream.read(b))!=-1) {
outputStream.write(b,0,i);
}
inputStream.close();
outputStream.flush();
outputStream.close();
return mapping.findForward("lookwork");
}
报java.lang.IllegalStateException: getOutputStream() has already been called for this respons错误
并且文件下载打开(保存)类型都为HTML Document
最好在我的程序里改。不到最后我不想把代码放到jsp里
我之前也这样修改过,虽然不报java.lang.IllegalStateException。但文件下载打开(保存)类型都为HTML Document ,而且我下载的文件只有一个pro_oa.sql文件能在网页里打开不出错。其他的文件无论是英文还是中文为命名的都直接调迅雷下载。下载下来的也都打不开。
我在这里追加分了!
展开
 我来答
nilaor
2010-05-17 · TA获得超过903个赞
知道小有建树答主
回答量:537
采纳率:0%
帮助的人:537万
展开全部
将最后一句的return mapping.findForward("lookwork");
修改为return null就不会有这个错误了。
原因:你的职前的代码中,已经调用了outputStream.close();语句将response的输出流给关闭了,下面再调用forwad的话,向response中写内容就会出错。
sh_wilson
2010-05-17
知道答主
回答量:4
采纳率:0%
帮助的人:0
展开全部
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%
java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null;
String forumid = request.getParameter("forumid");
String filepath = new String((request.getParameter("filepath"))
.getBytes("iso-8859-1"), "gbk");
String filename = new String((request.getParameter("filename"))
.getBytes("iso-8859-1"), "gbk");
try {
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition",
"attachment; filename="
+ new String(filename.getBytes("gbk"),
"iso8859-1"));
bis = new java.io.BufferedInputStream(
new java.io.FileInputStream(config.getServletContext()
.getRealPath(filepath)));
bos = new java.io.BufferedOutputStream(response
.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
%>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式