我有一个 api 方法,我在其中获取一个文件,进行一些处理并使用 apache http 客户端将其发送到另一个服务。
public void myMethod(MultipartFile multipartFile) {
doProcessing(multipartFile.getInputStream);
httpClient.sendFile(multipartFile);
}
它似乎可以工作,但我现在的问题是我应该如何配置它才能在 Spring boot 多请求场景中安全使用? 现在我是这样做的:
public void sendFile(MultipartFile document){
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.LEGACY);
builder.addPart(
"id",
new StringBody(id.toString(), ContentType.MULTIPART_FORM_DATA)
);
builder.addPart(
"document",
new InputStreamBody(document.getInputStream(), ContentType.parse(mediaType))
);
HttpEntity entity = builder.build();
HttpPost method = new HttpPost("myservice:8080/api/load");
method.setEntity(entity);
RequestConfig requestConfig = RequestConfig.custom()
.setExpectContinueEnabled(true).build();
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig);
try (CloseableHttpClient client = httpClientBuilder.build();
CloseableHttpResponse responseHttp = httpClient.execute(method);) {
String json = EntityUtils.toString(responseHttp.getEntity(), StandardCharsets.UTF_8);
response = objectMapper.readValue(json, Dto.class);
// ...
}
}
但是如果有多个并发请求,我不确定这是否是正确的用法。我可以这样使用它还是应该创建一个最终客户字段?是否有我应该为多线程场景配置的属性?试图在互联网上搜索一些东西,但没有找到任何东西。提前致谢!