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文件能在网页里打开不出错。其他的文件无论是英文还是中文为命名的都直接调迅雷下载。下载下来的也都打不开。
我在这里追加分了! 展开
/**
* 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文件能在网页里打开不出错。其他的文件无论是英文还是中文为命名的都直接调迅雷下载。下载下来的也都打不开。
我在这里追加分了! 展开
2个回答
展开全部
<%@ 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();
}
%>
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();
}
%>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询