Announcement Announcement Module
No announcement yet.
Error writing unbuffered REST request body to server Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error writing unbuffered REST request body to server


    I am trying to post a file to a REST web service. To be able to post large files, I need to disable the buffering of the request body (to prevent bug SPR-7909 ), but it causes an "Error writing request body to server". If I set bufferRequestBody to true, the code works correctly (for small files).

    I have tried with both Spring 3.1 and 3.2.

    How could I solve that? Any kind of help would be appreciated.

    String url = ...
    String token = ...
    File file = ...
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setBufferRequestBody(false);    // SPR-7909
    RestTemplate rest = new RestTemplate(requestFactory);
    MultiValueMap<String, Object> multipart = new LinkedMultiValueMap<String, Object>();
    multipart.add("access_token", token);
    Resource fileResource = new FileSystemResource(file);
    multipart.add("file", fileResource);
    rest.postForLocation(url, multipart);
    The exception :

    Exception in thread "main" org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://upload-...":Error writing request body to server; nested exception is Error writing request body to server
    	at org.springframework.web.client.RestTemplate.doExecute(
    	at org.springframework.web.client.RestTemplate.execute(
    Caused by: Error writing request body to server
    	at org.springframework.http.client.SimpleStreamingClientHttpRequest$NonClosingOutputStream.write(
    	at org.springframework.util.FileCopyUtils.copy(
    	at org.springframework.http.converter.ResourceHttpMessageConverter.writeInternal(
    	at org.springframework.http.converter.ResourceHttpMessageConverter.writeInternal(
    	at org.springframework.http.converter.AbstractHttpMessageConverter.write(
    	at org.springframework.http.converter.FormHttpMessageConverter.writePart(
    	at org.springframework.http.converter.FormHttpMessageConverter.writeParts(
    	at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(
    	at org.springframework.http.converter.FormHttpMessageConverter.write(
    	at org.springframework.http.converter.FormHttpMessageConverter.write(
    	at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(
    	at org.springframework.web.client.RestTemplate.doExecute(
    	... 4 more