使用Struts下载时,为什么下载rar.js文件时会把jsp页面关闭,而下载.doc,.xls文件却不会出现jsp页面自动关闭的现象?Java 的代码如下:请指教ServletOutputStream out = null;
try {
String dir = this.getServlet().getServletContext().getRealPath("/");
String path = dir + "\\finance\\jc\\文书格式\\" + name; File fileToDown = new File(path);
FileInputStream fileIn = new FileInputStream(fileToDown);
BufferedInputStream buffer = new BufferedInputStream(fileIn);
int fileLength = fileIn.available();
byte[] fileBytes = new byte[fileLength]; response.setContentLength(fileLength);
// response.setContentType("application/x-msdownload");
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename=jcbg.rar"); while (true) {
int isOver = buffer.read(fileBytes);
if (isOver == -1)
break;
}
buffer.close();
out = response.getOutputStream();
out = response.getOutputStream();
out.write(fileBytes);
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
try {
String dir = this.getServlet().getServletContext().getRealPath("/");
String path = dir + "\\finance\\jc\\文书格式\\" + name; File fileToDown = new File(path);
FileInputStream fileIn = new FileInputStream(fileToDown);
BufferedInputStream buffer = new BufferedInputStream(fileIn);
int fileLength = fileIn.available();
byte[] fileBytes = new byte[fileLength]; response.setContentLength(fileLength);
// response.setContentType("application/x-msdownload");
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename=jcbg.rar"); while (true) {
int isOver = buffer.read(fileBytes);
if (isOver == -1)
break;
}
buffer.close();
out = response.getOutputStream();
out = response.getOutputStream();
out.write(fileBytes);
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
response.setContentType("application/octet-stream; charset=utf-8");
把下面的这句注释掉,
把上面的注释拿下
response.setContentType(null);
如:<a href="DownloadServlet">下 载</a>其中DownloadServlet实现下载的Servlet