鉴于网上许多下载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();
            }
        }
    }

  

相关文章:

  • 2021-12-15
  • 2022-12-23
  • 2022-02-07
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-06
猜你喜欢
  • 2021-09-26
  • 2021-11-29
  • 2022-12-23
  • 2021-12-30
  • 2021-07-25
相关资源
相似解决方案