软件编程
位置:首页>> 软件编程>> java编程>> 关于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服务传输的问题

关于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服务传输的问题

作者:强强强啊丶  发布时间:2023-01-04 15:38:04 

标签:springboot,MultipartFile,RestTemplate,文件传输

大家好,因为近期做需求中遇到了文件上传这个东西,而且我这个还是跨服务去传输文件的所以我这边使用了httpclient和RestTemplate去做,但是最后还是用的httpclientfeign和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不会所以大家可以根据自己场景去选择。

关于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服务传输的问题

来源:https://blog.csdn.net/weixin_41914928/article/details/112249004

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com