zoukankan      html  css  js  c++  java
  • 下载根目录下的pdf文件, 浏览器下载

     public void outPut(HttpServletRequest request, HttpServletResponse response, Integer type) {
            ClassPathResource classPathResource = new ClassPathResource("pdf/通知.pdf");
            String filename = "通知.pdf";
            BufferedInputStream bufferedInputStream = null;
            BufferedOutputStream bufferedOutputStream = null;
            try {
                String userAgent = request.getHeader("User-Agent");
                response.setCharacterEncoding("utf-8");
                byte[] bytes = userAgent.contains("MSIE") ? filename.getBytes() : filename.getBytes("UTF-8");
                response.setContentType("application/pdf");
                response.addHeader("Content-Disposition",
                        "attachment;filename=" + new String(bytes, "iso8859-1"));
                bufferedInputStream = new BufferedInputStream(classPathResource.getInputStream());
                //bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
                byte[] bytes2 = new byte[1024];
                int i = 0;
                while ((i = bufferedInputStream.read(bytes2)) != -1){
                    response.getOutputStream().write(bytes2, 0, i);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    response.getOutputStream().flush();
                    response.getOutputStream().close();
                    //bufferedOutputStream.close();
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

     上面的被注销掉的两行代码,在没有注销之前,下载的pdf老是打不开。再去掉包装之后,文件可以正常打开。

  • 相关阅读:
    1121 Django基本
    1121 爬虫简单面条版
    1118 DOM
    1114 CSS基础
    1116 前端的练习--博客界面
    1112 前端基础之标签
    仿优酷错误
    1107 python自定义实现ORM
    cesm1_2_2在南信大大型机上的移植以及运行简单case的步骤
    ERROR:105: Unable to locate a modulefile for 'xxx'
  • 原文地址:https://www.cnblogs.com/prader6/p/12054394.html
Copyright © 2011-2022 走看看