文件下载后页面如何自动关闭
页面代码
<%@ page language= "java " contentType= "text/html; charset=GB2312 "
import= "java.net.*,java.io.* "
%> <%
response.setContentType( "application/x-download ");
String filename = request.getParameter( "filename ");
filename = new String(filename.getBytes( "iso8859-1 "), "gb2312 ");
String filenamedisplay = URLEncoder.encode(filename, "UTF-8 ");
String filenamedownload =this.getClass().getClassLoader().getResource( "/ ").getPath()+ "../../temp/ "+filename;
System.out.print(filenamedownload);
response.addHeader( "Content-Disposition ", "attachment;filename= " + filenamedisplay);
OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(filenamedownload);
byte[] b = new byte[1024];
int i = 0;
while((i = fis.read(b)) > 0)
{
output.write(b, 0, i);
}
output.flush();
}
catch(Exception e)
{
System.out.println( "Error! ");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
}
%>
------解决方案-------------------- <%@ page language= "java " contentType= "text/html; charset=GB2312 "
import= "java.net.*,java.io.* "
%>
把contentType= "text/html; charset=GB2312 "去掉了;
不过真正的原因是你到这个页面的链接有 _black吧,去掉就不打开新页了,只是弹出个下载框。关闭嘛,那是不可能的
------解决方案--------------------你就用a href 当点击某处时就会自动弹出下载框。完了会关闭。我这要代码。可以以10点来换
------解决方案--------------------代码末尾添加如下代码:
<script type= "text/javascript ">
<!--
window.close();
//-->
</script>
------解决方案--------------------下载完之后再关的话可能会有问题,就是你的response里面还有东西,可能会多下东西,你不防设下:当用户点下一个下载链接的时候你用新窗口打开,当执行到下载弹出下载对话框的时候再把这个新窗口关了,原来的窗口是否有无再关的必要你可以再考滤一下~