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提交不会有浏览器下载提示

  • 相关阅读:
    hdu 1045 Fire Net
    hdu 1044 Collect More Jewels
    hdu 1043 Eight
    hdu 1042 N!
    hdu 1041 Computer Transformation
    hdu 1040 As Easy As A+B
    CI在ngnix的配置
    angularjs表单验证checkbox
    chrome浏览器跨域设置
    angularjs向后台传参,后台收不到数据
  • 原文地址:https://www.cnblogs.com/zhoucx66/p/5553889.html
Copyright © 2011-2022 走看看