我们使用的是 RestEasy Client 3.0*。
我们正在尝试在请求正文中发布非常大的内容。
当内容长度小于 1 MB 时,以下代码运行正常。
当内容长度非常大(大约 500 MB)时,请求会卡住。
代码如下所示:
InputStream inputStream = new FileInputStream(tempFile);
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("<SomeUrl>");
response = target.request(MediaType.WILDCARD).post(Entity.entity(inputStream, MediaType.WILDCARD));
我应该如何更改代码以支持大型内容?
一周后......
由于没有人回答我 - 我使用了很好的旧 Spring 解决方案(见下文)。
Spring 比 RestEasy 客户端好吗?
InputStream inputStream = new FileInputStream(tempFile);
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setBufferRequestBody(false);
RestTemplate r = new RestTemplate(factory);
RequestCallback requestCallback = new RequestCallback() {
@Override
public void doWithRequest(ClientHttpRequest clientHttpRequest) throws IOException {
List<org.springframework.http.MediaType> MEDIA_TYPES = Collections.unmodifiableList(Arrays.asList(org.springframework.http.MediaType.ALL));
clientHttpRequest.getHeaders().setAccept(MEDIA_TYPES);
OutputStream requestOutputStream = clientHttpRequest.getBody();
try {
int copiedBytes = IOUtils.copy(inputStream, requestOutputStream);
} finally {
IOUtils.closeQuietly(requestOutputStream);
}
}
};
ResponseExtractor<Response> responseExtractor = new ResponseExtractor<Response>() {
@Override
public Response extractData(ClientHttpResponse response) throws IOException {
return Response.status(response.getRawStatusCode()).build();
}
};
response = r.execute(url, HttpMethod.POST, requestCallback, responseExtractor);