展开全部
1、response.reset();不应该去reset
2、你参考下我以前写的这段下载pdf的文件吧,稍改下肯定e68a84e8a2ad3231313335323631343130323136353331333332623963能解决楼主的问题了。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;
}