zoukankan      html  css  js  c++  java
  • android默认浏览器response下载PDF文件

      下载出来的文件不是PDF,而是xxx.htm文件,原因是response的header配置有问题。

    android默认浏览器的情况下,header的配置应该写成。(java 为例)

    response.setHeader("Content-Type", "application/pdf");

    response.setHeader("Content-Disposition", "attachment; filename="sample.pdf"");

    红字部分为要点。1,如果是PDF的情况下尽量用application/pdf,2文件名用双引号括起来(这一条没办法的时候可以试试,一般不影响,因为文件名一般不直接写进来),

    3 文件后缀名小写无效的情况下,把它变成大写再试(一般小写就有效,如果后缀是大写的话,会影响android文件自动识别,在打开文件时需要手动选择打开方式)。

    扩展下,下载所有符合二进制编码的邮件

    response.setHeader("Content-Type", "application/octet-stream"); // 这里变成application/octet-stream就好了。
    response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(inputfilename, "UTF-8")); // 这里的inputfilename例如test.doc,test.jar,test.pdf,test.html等等。不需要用双引号括起来。
    OutputStream out = response.getOutputStream();
    File file = new File(logfilepath + inputfilename);
    InputStream is = new FileInputStream(file);
    DownloadUtil.transfer(is, out);

    public class DownloadUtil {

    public static void transfer(InputStream is, OutputStream os) throws IOException {

    int count = 0;
    byte[] buffer = new byte[8192];
    while ((count = is.read(buffer)) > 0) {
    os.write(buffer, 0, count);
    }
    is.close();
    os.close();
    }
    }

  • 相关阅读:
    Python深拷贝和浅拷贝解析
    python中count函数的用法
    Jenkins + gitlab + maven 自动打包部署项目
    nio和bio得区别
    nginx负载均衡的5种策略
    接口测试常见bug
    接口自动化面试4
    pass 语句
    if 语句
    while循环
  • 原文地址:https://www.cnblogs.com/niutouzdq/p/3453722.html
Copyright © 2011-2022 走看看