1

我们使用的是 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);
4

1 回答 1

0

我猜在 Spring 4.0.1 中你可以使用 Apache HTTP 客户端和 HttpComponentsClientHttpRequestFactory 而不是 SimpleClientHttpRequestFactory。这是原始注释:SPR-10728

<bean id="restTemplateStreaming" class="org.springframework.web.client.RestTemplate">
    <constructor-arg>
        <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
            <property name="bufferRequestBody" value="false" />
            <property name="httpClient">
                <bean class="com.spotfire.server.http.spring.HttpClientFactory">
                    <property name="clientBuilder">
                        <bean class="org.apache.http.impl.client.HttpClientBuilder">
                            <property name="connectionManager" ref="poolingHttpClientConnectionManager" />
                            <property name="redirectStrategy">
                                <bean class="org.apache.http.impl.client.LaxRedirectStrategy" />
                            </property>
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    </constructor-arg>      
</bean>

注意但是回调实现也发生了变化。你不能再调用 clientHttpRequest.getBody(); 相反,您必须使用如下所示的有点复杂的结构:

class StreamingRequestCallback implements RequestCallback {

private final InputStream inputStream;

StreamingRequestCallback(InputStream inputStream) {
  this.inputStream = inputStream;
}

@Override
public void doWithRequest(final ClientHttpRequest request) throws IOException {
  request.getHeaders().add("Content-type", MediaType.APPLICATION_OCTET_STREAM_VALUE);

  ((StreamingHttpOutputMessage) request).setBody(new StreamingHttpOutputMessage.Body() {
    @Override
    public void writeTo(final OutputStream outputStream) throws IOException {
      IOUtils.copy(inputStream, outputStream);
    }
  });
}

};

于 2014-02-04T21:43:50.827 回答