springboot 中使用httpclient或RestTemplate做MultipartFile檔案傳輸
大家好,因為近期做需求中遇到了檔案上傳這個東西,而且我這個還是跨服務去傳輸檔案的所以我這邊使用了httpclient和RestTemplate去做,但是最后還是用的httpclient,feign和RestTemplate在超大檔案下會OOM所以適用于小檔案傳輸我這邊測驗的在1G以下,httpclient好像是無限哈哈哈,(具體多少大家有時間可以去測一下)
1.被呼叫服務的Controller
1.這塊使用@RequestParam(“file”)或者@RequestPart(“file”)都是可以接到引數的,
2.(“file”)一定要和遠程呼叫代碼傳的引數名一樣 否則接不到引數,
@RequestMapping(value = "/remoteCallUpload",method = RequestMethod.POST)
@ApiOperation("測驗遠程呼叫上傳")
public String remoteCallUpload(@RequestParam("file") MultipartFile file){
System.out.println(file);
return "成功";
}
1.RestTemplate
1.如果用RestTemplate的話首先需要把RestTemplate交給spring去管理所以先來一個配置類,
2.@SuppressWarnings(“all”) 這個注解是jdk自帶的的意思是 意志所有的警告,
@Configuration
@SuppressWarnings("all")
public class RestTemplateConfig {
@Autowired
RestTemplateBuilder builder;
@Bean
public RestTemplate restTemplate() {
return builder.build();
}
}
2.RestTemplate遠程呼叫檔案傳輸
這里有幾個要注意的地方
1.必須重寫否則傳輸時報錯
ByteArrayResource byteArrayResource = new ByteArrayResource(file.getBytes()) {
@Override
public String getFilename() {
return file.getOriginalFilename();
}
};
2.設定請求頭因為就在模擬前端發送上傳檔案的請求所以請求頭必須是multipart/form-data
3.第三個引數是被呼叫Controller的回傳值型別,我的測驗Controller寫的是String所以我的這邊第三引數就是String.Class
restTemplate.postForObject(url, files, String.class);
4.url就是被呼叫服務的地址 如:
http://192.168.3.7:50003/test/remoteCallUpload
以上是注意事項,
@Autowired
private RestTemplate restTemplate;
private String gettestRestTemplate(MultipartFile file, String url) throws IOException {
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
ByteArrayResource byteArrayResource = new ByteArrayResource(file.getBytes()) {
@Override
public String getFilename() {
return file.getOriginalFilename();
}
};
form.add("file", byteArrayResource);
form.add("filename", file.getOriginalFilename());
//用HttpEntity封裝整個請求報文
HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
String flag = restTemplate.postForObject(url, files, String.class);
return flag;
}
3.HttpClient
1.使用httpclient的話首先要引入pom檔案坐標,
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.6</version>
</dependency>
3.HttpClient遠程呼叫檔案傳輸
1.httpclient這段代碼有要用的小伙伴直接粘過去就能用
注意一下回傳值自己改一下就行execute.getEntity()
@SneakyThrows
private String gettesthttpclient(MultipartFile file, String url) {
CloseableHttpClient httpclient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(10000)
.setConnectTimeout(5000)
.build();
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
// 解決中文檔案名亂碼問題
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.setCharset(Consts.UTF_8);
ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), Consts.UTF_8);
entityBuilder.addBinaryBody("file", file.getInputStream(), ContentType.DEFAULT_BINARY, file.getOriginalFilename());
httpPost.setEntity(entityBuilder.build());
httpPost.setConfig(requestConfig);
HttpResponse execute = httpclient.execute(httpPost);
String flag = EntityUtils.toString(execute.getEntity());
return flag;
}
總結
遠程呼叫使用了RestTemplate和httpclient也可以使用feign,但是RestTemplate和feign大檔案會OOM,httpclient不會所以大家可以根據自己場景去選擇,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/245200.html
標籤:java
