用servlet实现的excel下载,在用浏览器直接下载时,获得的文件名是设置在header中的文件名,
但如果用迅雷下载,生成的文件名却成了servlet的名称,导致文件无法打开,
在网上搜索了下,response.setContentLength()和response.setContentType()可以解决,但我这就是不行啊
附上代码
OutputStream out = null;
//文件名转码
if(request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0 || request.getHeader("User-Agent").toLowerCase().indexOf("chrome") > 0){
fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");//火狐和谷歌
} else if(request.getHeader("User-Agent").toLowerCase().indexOf("MSIE") > 0){
fileName = URLEncoder.encode(fileName,"UTF-8");//IE
}
response.setHeader("Content-Disposition","attachment;filename=" + fileName);
response.setContentLength((int)file.length());
response.setcontentType("application/vnd.ms-excel;charset=utf-8");
out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesread = 0;
while((bytesread = bis.read(buff, 0, buff.length)) != -1){
bos.write(buff, 0 ,bytesread);
} catch (IOException e){
e.printStackTrace();
} finally {
out.flush();
// 关流什么的省略了
}
}
大概基本代码就是上面这些了,请各位看看是什么问题啊
------解决思路----------------------
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
String fileName = "", fileName1 = "", flag = "", filePath = "", logPath = "";
Object file = new Object();
FileInputStream fis = null;
BufferedOutputStream out = null;
try {
if (request.getParameter("filename") != null) {
fileName = request.getParameter("filename");
}
fileName1 = StringUtil.toUtf8String(fileName.substring(fileName.lastIndexOf("/") + 1));
response.setContentType("application/octet-stream");
response.setContentType("application/OCTET-STREAM;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName1);
filePath = InitParameter.getRealPath() + "\\" + InitParameter.getUploadFile_Path() + "\\";
file = new File(filePath + fileName);
try {
fis = new FileInputStream((File)file);
out = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
} catch (FileNotFoundException e) {
throw new AppException("您下载的文件文件不存在。");
}
} catch (Exception e) {
String msg = "";
response.reset();
response.setContentType("text/html;charset=UTF-8");
if (e.getLocalizedMessage() != null) {
msg = e.getMessage();
} else {
msg = "系统错误,捕捉的错误类型为" + e.getClass();
}
response.getWriter().println("<script>parent.downback('" + fileName + "','" + msg + "');</script>");
} finally {
try {
if (fis != null) {
fis.close();
}
if (out != null) {
out.close();
}
if (request.getParameter("del") != null) {
((File)file).delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
------解决思路----------------------
问题解决了吗?也遇到这个问题
response.setContentType("application/octet-stream");
response.setContentLength(fileLength);
关键设置都有了,迅雷下载就是不行
------解决思路----------------------
response.setContentType("application/force-download");
response.setHeader("Content-Disposition","attachment;filename="+file.getName());
response.setHeader("Content-Length",file.length()+"");
这样设置试试
------解决思路----------------------
我也被坑过 最后我就直接给文件路径下载