Hi, everyone. I’m using HttpClient and RestTemplate to transfer files across services, but I’m still using HttpClient. Feign and RestTemplate will be OOM for very large files, so it is suitable for small text transfer. Httpclient seems to be infinite hahaha. (The specific number can be measured if you have time)

1. Controller of the invoked service

1. @requestParam (” file “) or @requestPart (” file “) can be used to connect parameters. 2.(” file “) Must be the same as the parameter name passed by the remote calling code; otherwise, the parameter cannot be received.

@requestmapping (value = "/remoteCallUpload",method = requestmethod.post) @apioperation (" test remoteCallUpload") public String remoteCallUpload(@RequestParam("file") MultipartFile file){ System.out.println(file); Return C. }Copy the code

1.RestTemplate

1. If you use RestTemplate, you need to hand it over to Spring to manage, so start with a configuration class. 2.@SuppressWarnings(” all “) This annotation comes with the JDK and means all warnings.

@Configuration @SuppressWarnings("all") public class RestTemplateConfig { @Autowired RestTemplateBuilder builder; @Bean public RestTemplate restTemplate() { return builder.build(); }}Copy the code

2.RestTemplate remotely transfers files

There are a few caveats here

1. It must be overwritten or the transmission will report an error

ByteArrayResource byteArrayResource = new ByteArrayResource(file.getBytes()) { @Override public String getFilename() { return file.getOriginalFilename(); }};Copy the code

2. Set the request header. The request header must be multipart/form-data

3. The third argument is the return type of the Controller being called. My test Controller writes String, so my third argument is String.class

restTemplate.postForObject(url, files, String.class);
Copy the code

4. The url is the address of the service to be called.

http://192.168.3.7:50003/test/remoteCallUpload
Copy the code

These are the precautions.

@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<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers); String flag = restTemplate.postForObject(url, files, String.class); return flag; } 1234567891011121314151617181920212223Copy the code

3.HttpClient

1. To use HttpClient, first introduce poM file coordinates.

< the dependency > < groupId > org, apache httpcomponents < / groupId > < artifactId > httpclient < / artifactId > < version > 4.5.6 < / version > </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> The < version > 4.5.6 < / version > < / dependency > 12345678910Copy the code

3.HttpClient invokes file transfer remotely

1. The httpClient code has a partner that you can use to paste it in and change it to be execut.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(); / / solve the problem of Chinese filenames garbled 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; }Copy the code

conclusion

Remote calls using RestTemplate and HttpClient can also use Feign, but RestTemplate and Feign large files will be OOM, httpClient will not, so you can choose according to your scenario.