zoukankan      html  css  js  c++  java
  • HttpServletResponse设置下载文件

        // path是指欲下载的文件的路径。
                File file = new File(path);
                // 取得文件名。
                String filename = file.getName();
                filename = new String(filename.getBytes("GB2312"),"ISO-8859-1");
                // 以流的形式下载文件。
                InputStream fis = new BufferedInputStream(new FileInputStream(path));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                // 清空response
                response.reset();
                // 设置response的Header
                response.addHeader("Content-Disposition", "attachment;filename="
                        + filename);
                response.addHeader("Content-Length", "" + file.length());
                OutputStream stream = new BufferedOutputStream(
                        response.getOutputStream());
                response.setContentType("application/vnd.ms-excel;charset=gb2312");
                stream.write(buffer);
                stream.flush();
                fis.close();
                stream.close();

      注:只能以form方式提交浏览器才有下载提示,ajax提交不会有浏览器下载提示

  • 相关阅读:
    最好的云备份选项
    不要让你的云备份策略退居次位
    了解区域类型
    Managing WMI security
    创建、导入、导出、复制以及粘贴 WMI 筛选器
    Gpfixup
    centos 7 安装MySQL 5.7.23
    centos 7 配置 mysql 5.7 主从复制
    oracle DG搭建
    undo表空间丢失、损坏
  • 原文地址:https://www.cnblogs.com/zhoucx66/p/5553889.html
Copyright © 2011-2022 走看看