java web服务器文件的下载(有下载弹出匡)


昨天做了一个文件从服务下载的功能,怎么都不弹出页面,下载框。后查询得知。目前两种方法

1.<a href='下载路径' />

2.window.location.href = basePath + "downloadTemplate.do?template=huiyuan";

通过 这两种方式可以有这种弹出窗

希望能帮助,遇到和我一样问题的朋友~

 

 

附下载的工具类,可直接复制可用

public void downloadFile(String filePath,String fileName,HttpServletResponse response) throws Exception{
        //URI uri =  this.getClass().getClassLoader().getResource(filePath).toURI();
        File file = new File(filePath);
        // 清空response
        response.reset();
        // 设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename="
                + new String(fileName.getBytes("gbk"), "iso-8859-1")); // 转码之后下载的文件不会出现中文乱码
        response.addHeader("Content-Length", "" + file.length());
        // 以流的形式下载文件
        
        InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        OutputStream toClient = new BufferedOutputStream( response.getOutputStream());
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
       }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM