zoukankan      html  css  js  c++  java
  • Java:原生javaWeb下载pdf文件

    鉴于网上许多下载pdf的代码下载的pdf都是无效pdf,我稍加修改:

        @RequestMapping("/downPdf")
        public void downPdf(HttpServletResponse response, HttpServletRequest request){
            String pdfPath = "C:\Users\17921\Desktop\spring-boot-reference.pdf";
            File file = new File(pdfPath);
            String pdfName = FilenameUtils.getName(pdfPath);
            OutputStream os = null;
            try{
                FileInputStream in = new FileInputStream(file);
                BufferedInputStream bf = new BufferedInputStream(in);
                os = response.getOutputStream();
                response.setContentType(MediaType.APPLICATION_PDF_VALUE);
                response.setHeader("content-Disposition","attachment;filename="+pdfName);
                byte[] b = new byte[bf.available() + 1000];
                int i;
                while((i=bf.read(b))!=-1){
                    os.write(b,0,i);
                }
                os.flush();
            }catch(IOException ignored){
    
            }finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    

      

  • 相关阅读:
    Android开发环境
    安卓学习
    Shuffle'm Up POJ
    Duizi and Shunzi HDU
    Find a path HDU
    Cyclic Nacklace HDU
    Keywords Search HDU
    HDU 1495 非常可乐
    J
    Fire Game FZU
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/13501864.html
Copyright © 2011-2022 走看看