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