Java實現(xiàn)網(wǎng)絡(luò)文件下載以及下載到指定目錄
前言
最近項目中有使用到pdf文件下載功能,在使用過程中,發(fā)現(xiàn)spring給我們提供了兩個工具類,已經(jīng)實現(xiàn)了文件下載的功能:
StreamUtils和FileCopyUtils,都是org.springframework.util包下面的類,使用非常方便。
相關(guān)代碼
service實現(xiàn)類
public InputStream getDownloadInputStreamByUrl(String downloadUrl) { if (ObjectUtils.isEmpty(downloadUrl)) { throw new RuntimeException("無效參數(shù):fileUrl 不能為空"); } InputStream in = null; try { URL url = new URL(downloadUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 設(shè)置超時時間為3秒 conn.setConnectTimeout(3 * 1000); // 防止屏蔽程序抓取而返回403錯誤 conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 得到輸入流 in = conn.getInputStream(); } catch (Exception e) { throw new RuntimeException(e); } return in; }
Controller類
@GetMapping("/fileDownload/{filename}") public void downloadPdfFile(HttpServletResponse response, @PathVariable(name="filename") String filename) { try { if (StringUtils.isEmpty(filename)) { throw new RuntimeException("文件名不可為空!"); } //通過文件名查找文件對應(yīng)的url String downloadUrl = fileDownloadService.getUrlByFiname(filename); if (StringUtils.isEmpty(filename)) { throw new RuntimeException("文件對應(yīng)地址不存在"); } // 此處異常方法中已處理 InputStream in = fileDownloadService.getDownloadInputStreamByUrl(downloadUrl); // 設(shè)置編碼格式 response.setCharacterEncoding("UTF-8"); /* 若要下載指定目錄,以下兩行需注釋,否則會下載兩份,一份到指定目錄,一份到瀏覽器下載目錄 */ // 設(shè)置下載格式 response.setHeader("Content-Type", "application/pdf"); // 注釋這行代碼,可實現(xiàn) 預(yù)覽 功能(只限pdf文件) response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); //下載到指定目錄 /* File file = new File("C:\\tmp"); if (!file.exists()) { //如果文件夾不存在,則創(chuàng)建新的的文件夾 file.mkdirs(); } File file1 = new File(file.getPath() + File.separator + filename); OutputStream outputStream = new FileOutputStream(file1); FileCopyUtils.copy(in, outputStream); */ StreamUtils.copy(in, response.getOutputStream()); } catch (Exception e) { throw new RuntimeException(e); } }
此處通過spring提供的工具類進行文件下載。
工具類中實現(xiàn)方式:
此處也可通過FileCopyUtils.copy(InputStream input, OutputStream output)實現(xiàn)文件下載,其底層也是通過調(diào)用StreamUtils.copy(InputStream in, OutputStream out)方法實現(xiàn)的,不過此方法最后通過finally關(guān)閉了InputStream和OutputStream,邏輯更嚴(yán)謹(jǐn)。
這兩個工具類中還有其他的導(dǎo)出方法,可以傳遞其他參數(shù)進去,如圖:
可以看到,這兩個工具類中基本涵蓋了文件下載所需的所有操作。
以上代碼中對異常只是做了簡單的 throw new RuntimeException() 操作,讀者可根據(jù)自己的項目對異常進行處理。
總結(jié)
這些僅為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis Plus ActiveRecord模式的具體使用
ActiveRecord 是一種設(shè)計模式,它是一種在軟件開發(fā)中用于管理關(guān)系數(shù)據(jù)庫的模式,本文主要介紹了Mybatis Plus ActiveRecord模式的具體使用,具有一定的參考價值,感興趣的可以了解一下2024-07-07SpringBoot?AOP?@Pointcut切入點表達式排除某些類方式
這篇文章主要介紹了SpringBoot?AOP?@Pointcut切入點表達式排除某些類方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11解決rror updating database.Cause:java.sql.SQLSyntaxE
這篇文章主要介紹了解決rror updating database.Cause:java.sql.SQLSyntaxErrorException問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05GC算法實現(xiàn)篇之并發(fā)標(biāo)記清除
這篇文章主要為大家介紹了GC算法實現(xiàn)篇之并發(fā)-標(biāo)記-清除,?CMS垃圾收集器在減少停頓時間上做了很多給力的工作,?大量的并發(fā)線程執(zhí)行的工作并不需要暫停應(yīng)用線程2022-01-01SpringBoot結(jié)合ProGuard實現(xiàn)代碼混淆(最新版)
這篇文章主要介紹了SpringBoot結(jié)合ProGuard實現(xiàn)代碼混淆(最新版),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10