您的位置:程序门 -> java -> web 开发



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


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


文件下载后页面如何自动关闭
发表于: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;
        }
}

%>
发表于:2007-08-01 10:01:541楼 得分:0
没人回答?
发表于:2007-08-01 10:09:432楼 得分:0
<%@   page   language= "java "   contenttype= "text/html;   charset=gb2312 "
        import= "java.net.*,java.io.* "
%>
把contenttype= "text/html;   charset=gb2312 "去掉了;
不过真正的原因是你到这个页面的链接有   _black吧,去掉就不打开新页了,只是弹出个下载框。关闭嘛,那是不可能的
发表于:2007-08-01 14:25:383楼 得分:0
我用的是window.open()打开的,你说的是用 <a   href= " "> </a> 方式吗?
发表于:2007-08-01 15:54:084楼 得分:0
你就用a   href   当点击某处时就会自动弹出下载框。完了会关闭。我这要代码。可以以10点来换
发表于:2007-08-01 17:25:585楼 得分:0
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;
        }
}
%>
发表于:2007-08-01 18:00:206楼 得分:0
代码末尾添加如下代码:
<script   type= "text/javascript ">
<!--
window.close();  
//-->
</script>

发表于:2007-08-02 00:14:337楼 得分:0
下载完之后再关的话可能会有问题,就是你的response里面还有东西,可能会多下东西,你不防设下:当用户点下一个下载链接的时候你用新窗口打开,当执行到下载弹出下载对话框的时候再把这个新窗口关了,原来的窗口是否有无再关的必要你可以再考滤一下~
发表于:2007-08-02 09:25:178楼 得分:0
kingdom_seu:加javascript会报错
发表于:2007-08-02 11:15:099楼 得分:0
qinxiaofei(路漫漫)   (   )   信誉:100     2007-08-01   17:25:58     得分:   0    

经测试,这个可行


快速检索

最新资讯
热门点击