下载时Jsp页面自己关闭后,才出现下载的对话框
使用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();
}
------解决方案--------------------//response.setContentType( "application/x-msdownload ");
response.setContentType( "application/octet-stream; charset=utf-8 ");
把下面的这句注释掉,
把上面的注释拿下
------解决方案--------------------楼上说的那么就是反过来了,干脆就写成这样好了
response.setContentType(null);
------解决方案--------------------不设置连接的 target 属性,应该不会出现页面
如: <a href= "DownloadServlet "> 下 载 </a>
其中DownloadServlet实现下载的Servlet