文件下载后页面如何自动关闭

qinxiaofei 2007-08-01 08:49:45
页面代码

<%@ 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;
}
}

%>
...全文
621 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yijie1206688 2007-08-02
  • 打赏
  • 举报
回复
下载完之后再关的话可能会有问题,就是你的response里面还有东西,可能会多下东西,你不防设下:当用户点下一个下载链接的时候你用新窗口打开,当执行到下载弹出下载对话框的时候再把这个新窗口关了,原来的窗口是否有无再关的必要你可以再考滤一下~
qinxiaofei 2007-08-02
  • 打赏
  • 举报
回复
qinxiaofei(路漫漫) ( ) 信誉:100 2007-08-01 17:25:58 得分: 0

经测试,这个可行
qinxiaofei 2007-08-02
  • 打赏
  • 举报
回复
kingdom_seu:加javascript会报错
easyroom 2007-08-01
  • 打赏
  • 举报
回复
<%@ page language="java" contentType="text/html; charset=GB2312"
import="java.net.*,java.io.*"
%>
把contentType="text/html; charset=GB2312"去掉了;
不过真正的原因是你到这个页面的链接有 _black吧,去掉就不打开新页了,只是弹出个下载框。关闭嘛,那是不可能的
qinxiaofei 2007-08-01
  • 打赏
  • 举报
回复
没人回答?
kingdom_seu 2007-08-01
  • 打赏
  • 举报
回复
代码末尾添加如下代码:
<script type="text/javascript">
<!--
window.close();
//-->
</script>

qinxiaofei 2007-08-01
  • 打赏
  • 举报
回复
liubinwyzbt() ( ) 你说的是这样?
某jsp:
<a href="download.jsp?filename='xxx.doc'">xxx.doc</a>

download.jsp:
<%@ page language="java" 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;
}
}
%>
liubinwyzbt 2007-08-01
  • 打赏
  • 举报
回复
你就用a href 当点击某处时就会自动弹出下载框。完了会关闭。我这要代码。可以以10点来换
qinxiaofei 2007-08-01
  • 打赏
  • 举报
回复
我用的是window.open()打开的,你说的是用<a href=""></a>方式吗?

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧