SpringBoot實(shí)現(xiàn)文件下載的四種方式
1. 將文件以流的形式一次性讀取到內(nèi)存,通過響應(yīng)輸出流輸出到前端
/** * @param path 想要下載的文件的路徑 * @param response * @功能描述 下載文件: */ @RequestMapping("/download") public void download(String path, HttpServletResponse response) { try { // path是指想要下載的文件的路徑 File file = new File(path); log.info(file.getPath()); // 獲取文件名 String filename = file.getName(); // 獲取文件后綴名 String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase(); log.info("文件后綴名:" + ext); // 將文件寫入輸入流 FileInputStream fileInputStream = new FileInputStream(file); InputStream fis = new BufferedInputStream(fileInputStream); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 設(shè)置response的Header response.setCharacterEncoding("UTF-8"); //Content-Disposition的作用:告知瀏覽器以何種方式顯示響應(yīng)返回的文件,用瀏覽器打開還是以附件的形式下載到本地保存 //attachment表示以附件方式下載 inline表示在線打開 "Content-Disposition: inline; filename=文件名.mp3" // filename表示文件的默認(rèn)名稱,因?yàn)榫W(wǎng)絡(luò)傳輸只支持URL編碼的相關(guān)支付,因此需要將文件名URL編碼后進(jìn)行傳輸,前端收到后需要反編碼才能獲取到真正的名稱 response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); // 告知瀏覽器文件的大小 response.addHeader("Content-Length", "" + file.length()); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); outputStream.write(buffer); outputStream.flush(); } catch (IOException ex) { ex.printStackTrace(); } }
1.1 下載文件
@RequestMapping("getFile") @ResponseBody public void getFile(HttpServletResponse response) throws Exception{ File readFile = new File("/home/ssx/Music/CloudMusic/夜曲-周杰倫.flac"); //字節(jié)流-用于讀文件 這里只是demo用的非緩沖流。實(shí)際項(xiàng)目可以用BufferedInputStream。 此功能是讀取圖片,所以用的字節(jié)流。如果是文本的話可以用字符流效率高,具體類看下面注釋 // BufferedReader bufferedReader = new BufferedReader(new FileReader(readFile));//字符流 FileInputStream fileInputStream = new FileInputStream(readFile);//字節(jié)流 //設(shè)置文件下載方式 response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("夜曲-周杰倫.flac","utf-8")); //獲取servlet響應(yīng)輸出字節(jié)流 // PrintWriter writer = response.getWriter();//字符流 ServletOutputStream outputStream = response.getOutputStream(); //字節(jié)流 //流數(shù)據(jù)交換,每次交換10k數(shù)據(jù)大小 (1024k = 1m) byte[] bytes = new byte[1024*10]; int read; do { read = fileInputStream.read(bytes); outputStream.write(bytes,0,read); }while (-1 != read); //關(guān)閉資源 IOUtils.closeQuietly(fileInputStream); IOUtils.closeQuietly(outputStream); }
2. 將輸入流中的數(shù)據(jù)循環(huán)寫入到響應(yīng)輸出流中,而不是一次性讀取到內(nèi)存,通過響應(yīng)輸出流輸出到前端
/** * @param path 指想要下載的文件的路徑 * @param response * @功能描述 下載文件:將輸入流中的數(shù)據(jù)循環(huán)寫入到響應(yīng)輸出流中,而不是一次性讀取到內(nèi)存 */ @RequestMapping("/downloadLocal") public void downloadLocal(String path, HttpServletResponse response) throws IOException { // 讀到流中 InputStream inputStream = new FileInputStream(path);// 文件的存放路徑 response.reset(); response.setContentType("application/octet-stream"); String filename = new File(path).getName(); response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); ServletOutputStream outputStream = response.getOutputStream(); byte[] b = new byte[1024]; int len; //從輸入流中讀取一定數(shù)量的字節(jié),并將其存儲(chǔ)在緩沖區(qū)字節(jié)數(shù)組中,讀到末尾返回-1 while ((len = inputStream.read(b)) > 0) { outputStream.write(b, 0, len); } inputStream.close(); }
3. 下載網(wǎng)絡(luò)文件到本地
/** * @param path 下載后的文件路徑和名稱 * @param netAddress 文件所在網(wǎng)絡(luò)地址 * @功能描述 網(wǎng)絡(luò)文件下載到服務(wù)器本地 */ @RequestMapping("/netDownloadLocal") public void downloadNet(String netAddress, String path) throws IOException { URL url = new URL(netAddress); URLConnection conn = url.openConnection(); InputStream inputStream = conn.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream(path); int bytesum = 0; int byteread; byte[] buffer = new byte[1024]; while ((byteread = inputStream.read(buffer)) != -1) { bytesum += byteread; System.out.println(bytesum); fileOutputStream.write(buffer, 0, byteread); } fileOutputStream.close(); }
4. 網(wǎng)絡(luò)文件獲取到服務(wù)器后,經(jīng)服務(wù)器處理后響應(yīng)給前端
/** * @param netAddress * @param filename * @param isOnLine * @param response * @功能描述 網(wǎng)絡(luò)文件獲取到服務(wù)器后,經(jīng)服務(wù)器處理后響應(yīng)給前端 */ @RequestMapping("/netDownLoadNet") public void netDownLoadNet(String netAddress, String filename, boolean isOnLine, HttpServletResponse response) throws Exception { URL url = new URL(netAddress); URLConnection conn = url.openConnection(); InputStream inputStream = conn.getInputStream(); response.reset(); response.setContentType(conn.getContentType()); if (isOnLine) { // 在線打開方式 文件名應(yīng)該編碼成UTF-8 response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(filename, "UTF-8")); } else { //純下載方式 文件名應(yīng)該編碼成UTF-8 response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); } byte[] buffer = new byte[1024]; int len; OutputStream outputStream = response.getOutputStream(); while ((len = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, len); } inputStream.close(); }
到此這篇關(guān)于SpringBoot實(shí)現(xiàn)文件下載的四種方式的文章就介紹到這了,更多相關(guān)SpringBoot 文件下載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)文件的上傳、下載和預(yù)覽功能
- SpringBoot實(shí)現(xiàn)文件下載的限速功能
- SpringBoot上傳下載文件+oss實(shí)例
- SpringBoot中實(shí)現(xiàn)文件上傳、下載、刪除功能的步驟
- Vue2+SpringBoot實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到csv文件并下載的使用示例
- SpringBoot+MinIO實(shí)現(xiàn)文件上傳、讀取、下載、刪除的使用示例
- SpringBoot+ruoyi框架文件上傳和下載的實(shí)現(xiàn)
- SpringBoot返回文件使前端下載的幾種方式小結(jié)
相關(guān)文章
Shiro中session超時(shí)頁面跳轉(zhuǎn)的處理方式
這篇文章主要介紹了Shiro中session超時(shí)頁面跳轉(zhuǎn)的處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Java中的轉(zhuǎn)換流InputStreamReader解讀
InputStreamReader是Java.io包中的一個(gè)類,用于將字節(jié)輸入流轉(zhuǎn)換為字符輸入流,它繼承自java.io.Reader類,提供了兩種構(gòu)造方法,可以使用默認(rèn)或指定字符集創(chuàng)建實(shí)例,常用方法包括讀取字符、判斷是否準(zhǔn)備好讀取數(shù)據(jù)和關(guān)閉流2024-09-09SpringBoot2.x配置多數(shù)據(jù)源方式
這篇文章主要介紹了SpringBoot2.x配置多數(shù)據(jù)源方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03SpringBoot多文件分布式上傳功能實(shí)現(xiàn)
本文詳細(xì)介紹了如何在SpringBoot中實(shí)現(xiàn)多文件分布式上傳,并用代碼給出了相應(yīng)的實(shí)現(xiàn)思路和實(shí)現(xiàn)步驟,感興趣的朋友跟隨小編一起看看吧2023-06-06IntelliJ?IDEA?2022.2?正式發(fā)布新功能體驗(yàn)
IntelliJ?IDEA?2022.2為遠(yuǎn)程開發(fā)功能帶來了多項(xiàng)質(zhì)量改進(jìn),使其更美觀、更穩(wěn)定,新版本還具有多項(xiàng)值得注意的升級和改進(jìn),下面跟隨小編一起看看IDEA?2022.2新版本吧2022-08-08怎樣將一個(gè)JAR包添加到Java應(yīng)用程序的Boot?Classpath中
本文文章給大家介紹如何將一個(gè)JAR包添加到Java應(yīng)用程序的Boot?Classpath中,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的的朋友參考下吧2023-11-11