java文件下载出错
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletE...
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); // String path = request.getParameter("path"); String path = "F:\\OKDownLoad\\854546_e472e815bf086a04a78173d95c90b760_1369553818.gif"; //读到流中 InputStream ins = new FileInputStream(path); //设置输出的格式 response.reset(); response.setContentType("application/x-msdownload"); response.addHeader("Content-Disposition", "attachment; filename=\"下载测试\""); //循环取出流中的数据 byte[] b = new byte[100]; int len = 0; OutputStream os = response.getOutputStream(); try { while((len = ins.read(b)) > 0){ os.write(b, 0, len); } ins.close(); } catch (Exception e) { e.printStackTrace(); } }
点击现在就会出现以下提示:
请问这是为什么 展开
response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); // String path = request.getParameter("path"); String path = "F:\\OKDownLoad\\854546_e472e815bf086a04a78173d95c90b760_1369553818.gif"; //读到流中 InputStream ins = new FileInputStream(path); //设置输出的格式 response.reset(); response.setContentType("application/x-msdownload"); response.addHeader("Content-Disposition", "attachment; filename=\"下载测试\""); //循环取出流中的数据 byte[] b = new byte[100]; int len = 0; OutputStream os = response.getOutputStream(); try { while((len = ins.read(b)) > 0){ os.write(b, 0, len); } ins.close(); } catch (Exception e) { e.printStackTrace(); } }
点击现在就会出现以下提示:
请问这是为什么 展开
1个回答
展开全部
1、response.reset();不应该去reset
2、你参考下我以前写的这段下载pdf的文件吧,稍改下肯定能解决楼主的问题了。
ServletOutputStream out = res.getOutputStream();
// 用于解决下载后的中文乱码
String contentType = getContentType(fileExt);
// contentType=contentType+";charset=gbk";
res.setContentType(contentType);
System.out.println("显示下载的名称是:-------------------------------"
+ temp);
String contentHeader = "attachment" + ";" + "filename=\""
+ URLEncoder.encode(temp, "UTF-8") + "\"";
res.setHeader("Content-disposition", contentHeader);
System.out.println("contentHeader is =" + contentHeader);
// int MAX_FILE_SIZE = 3 * 1024 * 1024; //此处设置文件最大尺寸,对本程序,不设置即可,3M
int byte_unit = 1024;
// byte[] buffer = new byte[MAX_FILE_SIZE];
byte[] buffer = new byte[byte_unit];// 一次性读取文件的数据长度
int length = 0;
while ((length = is.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
if (is != null)
is.close();
if (out != null) {
out.flush();
out.close();
}
// 获得内容类型
private static String getContentType(String fileExt) {
String contentType = " ";
if (fileExt == null)
contentType = " ";
else if (fileExt.equals("doc"))
contentType = "application/msword ";
else if (fileExt.equals("pdf"))
contentType = "application/pdf";
else if (fileExt.equals("jpg"))
contentType = "image/jpeg";
else if (fileExt.equals("gif"))
contentType = "image/gif ";
else if (fileExt.equals("bmp"))
contentType = "image/bmp ";
else if (fileExt.equals("txt"))
contentType = "text/plain";
else if (fileExt.equals("xls"))
contentType = "application/-excel";
return contentType;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询