SpringBoot使用RestTemplate如何通過http請求將文件下載到本地
背景
最近被安排了一個活,純體力的重復性工作,將開發(fā)一個項目的指定資源通過現(xiàn)有的下載接口下載下來。
思路
因為沒有提供批量下載接口,同時下載的資源需要自己篩選,想著這樣人工處理特別麻煩,個人也沒有什么進步,就想著寫一段代碼處理,這樣處理起來準確,效率高,后續(xù)有類似的任務(wù)還可以將這段代碼改改后繼續(xù)使用。
1.篩選
篩選出需要的下載文件的id,這個可以根據(jù)業(yè)務(wù)調(diào)整,不一定是id,具體的實現(xiàn)根據(jù)業(yè)務(wù)邏輯實現(xiàn);
2.下載
下載方式有兩種,一種是通過postman或者apifox這種工具,通過提前構(gòu)建參數(shù)和提供寫有參數(shù)的文件,批量出發(fā)請求,這種只是大致想到的可以不靠編碼實現(xiàn)的方式;另外一種是借助后端代碼發(fā)起http請求,將下載的資源寫入本地文件中。下面簡單介紹第二種,需要時可以批量下載文件。
實現(xiàn)方式
- 資源下載方法
public static void downloadFile(String url, HttpHeaders headers, RequestParams requestParams, String outputPath) throws IOException { // 創(chuàng)建請求實體 HttpEntity<RequestParams> entity = new HttpEntity<>(requestParams, headers); // 初始化RestTemplate,也可以自己構(gòu)建配置類。 final RestTemplate restTemplate = new RestTemplate(); // 請求配置 ResponseEntity<Resource> response = restTemplate.exchange( URI.create(url), HttpMethod.POST, entity, Resource.class ); // 請求成功校驗 if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { // 獲取資源 Resource resource = response.getBody(); // 確保目錄存在 File outputFile = new File(outputPath); File parentDir = outputFile.getParentFile(); // 文件存在校驗 if (parentDir != null && !parentDir.exists()) { if (!parentDir.mkdirs()) { throw new IOException("Failed to create directory: " + parentDir); } } //獲取流 try (InputStream inputStream = resource.getInputStream(); OutputStream outputStream = Files.newOutputStream(outputFile.toPath())) { StreamUtils.copy(inputStream, outputStream); } } else { throw new IOException("Failed to download file: " + response.getStatusCode()); } }
- 實體類(可以自己定義)
@Data public class RequestParams implements Serializable { private Long id; }
- 函數(shù)調(diào)用
public static void downloadPerPaper(PaperInfo paperInfos) throws IOException { //url String url="https://xxxx.com"; //請求頭 HttpHeaders headers = new HttpHeaders(); headers.set("xxx","xxxxx"); //請求參數(shù) final RequestParams requestParams = new RequestParams(); requestParams.setId(1L); //存在存放地址 String path="xx/xx/xx.doc"; //調(diào)用下載方法 downloadFile(url,headers,requestParams,path); }
異常處理
Exception in thread "main" java.nio.file.AccessDeniedException: xx/xx/xx
出現(xiàn)這種報錯是必須指定的具體的文件,而不是文件夾,同時盡量保證存放文件的文件夾必須存在
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot使用RestTemplate發(fā)送http請求的實操演示
- SpringBoot使用RestTemplate實現(xiàn)HTTP請求詳解
- springboot中RestTemplate發(fā)送HTTP請求的實現(xiàn)示例
- springboot中RestTemplate配置HttpClient連接池詳解
- 基于springboot的RestTemplate、okhttp和HttpClient對比分析
- SpringBoot 利用RestTemplate http測試
- 關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯栴}
相關(guān)文章
Springboot2.0配置JPA多數(shù)據(jù)源連接兩個mysql數(shù)據(jù)庫方式
這篇文章主要介紹了Springboot2.0配置JPA多數(shù)據(jù)源連接兩個mysql數(shù)據(jù)庫方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09詳解Java中Array和ArrayList的比較和轉(zhuǎn)換
在 Java 編程中,arrays 和 arraylists 都是基本的數(shù)據(jù)結(jié)構(gòu),用來存放數(shù)據(jù)集合,雖然兩者的用途一樣,但是它們的特點極大地影響應(yīng)用的性能和靈活性,本文探討 arrays 和 arraylists 的重要特性,它們各自的強項和弱點,,需要的朋友可以參考下2023-08-08Spring?Boot?Actuator?漏洞利用小結(jié)
spring對應(yīng)兩個版本,分別是Spring Boot 2.x和Spring Boot 1.x,因此后面漏洞利用的payload也會有所不同,這篇文章主要介紹了Spring?Boot?Actuator?漏洞利用小結(jié),需要的朋友可以參考下2023-11-11