java 的在線下載文件 .pdf


 java  的在線下載文件  .pdf

1.下載資源的本地位置

 
         

2.設置響應頭

 
         

3.下載代碼




1
PeriodicalResource periodicalResource = periodicalResourceService.get(id); 2 String filePath = periodicalResource.getAttachment();//獲取資源位置 3 File file = new File(periodicalBaseDir + filePath);//本地資源位置 4 if (file.exists()) { 5 response.setContentType("application/force-download");// 設置強制下載不打開 6 response.addHeader("Content-Disposition", 7 "attachment;fileName=" + filePath.split("/")[filePath.split("/").length-1]);// 設置文件名 8 byte[] buffer = new byte[1024]; 9 FileInputStream fis = null; 10 BufferedInputStream bis = null; 11 try { 12 fis = new FileInputStream(file); 13 bis = new BufferedInputStream(fis); 14 OutputStream os = response.getOutputStream(); 15 int i = bis.read(buffer); 16 while (i != -1) { 17 os.write(buffer, 0, i); 18 i = bis.read(buffer); 19 } 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } finally { 23 if (bis != null) { 24 try { 25 bis.close(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 } 30 if (fis != null) { 31 try { 32 fis.close(); 33 } catch (IOException e) { 34 e.printStackTrace(); 35 } 36 } 37 } 38 }

 

 自己之前沒有做過在線現在的  項目中用到了  就讓大神發了一份代碼  看了看  自己研究一下                       

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM