zoukankan      html  css  js  c++  java
  • jsp页面下载文件

    .将创建的excel文档转换成需要输出的流:可以是文件流放在硬盘中,也可以是输出流输出到浏览器供下载。  ◆  文件流:FileOutputStream

    1         FileOutputStream fos = new FileOutputStream("F://workbook.xls");
    2         workBook.write(fos);
    3         fos.close();

      ◆  输出流 :response.getOutputStream()

    1       response.reset();
    2         response.setContentType("application/vnd.ms-excel;charset=gbk");
    3         response.setHeader("Content-Disposition", "attachment;filename=account.xls");
    4         OutputStream out = response.getOutputStream();
    5         workBook.write(out);
    6         out.close();  

      ◆ tip:

        *response.reset();清除首部的空白行
             * getResponse的getWriter()方法连续两次输出流到页面的时候,第二次的流会包括第一次的流,
             * 所以可以使用response.reset或者resetBuffer的方法。
             * resetBuffer方法与reset方法的区别是,头和状态码没有清除。

             如果发现这样设置后浏览器端并没有弹出【文件另存为】选择路径窗口,请检查下前面代码是否出现:

      1.response.setContentType("text/html;charset=UTF-8");//设置编码格式
          2.PrintWriter out = response.getWriter();导致无法确定输出流

    3、excel文件名为中文时乱码或者出现未知文件类型错误时,考虑用URLEncoder对文件名进行转码

       

      

    1         String name = java.net.URLEncoder.encode(fileName, "utf-8");
    2         response.setContentType("application/vnd.ms-excel;charset=utf-8");
    3         response.setHeader("Content-Disposition", "attachment;filename="+name.toString()+".xls"); 
    4         OutputStream out = response.getOutputStream();
    5         workBook.write(out);
    6         out.close();     

      

  • 相关阅读:
    python接口自动化-json数据处理
    Fiddler-抓取手机app请求
    monkey常用命令实例
    python接口自动化-Cookie_绕过验证码登录
    python接口自动化-session_自动发文
    python接口自动化-post请求4
    python接口自动化-post请求3
    Python学习笔记第十五周
    Python学习笔记第十四周
    Python学习笔记第十二周
  • 原文地址:https://www.cnblogs.com/caidaxian/p/6483334.html
Copyright © 2011-2022 走看看