Error writing unbuffered REST request body to server
Hello
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.
Code:
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 :
Code:
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 java.io.IOException: Error writing request body to server
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:499)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:447)
...
Caused by: java.io.IOException: Error writing request body to server
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(HttpURLConnection.java:2716)
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:2699)
at org.springframework.http.client.SimpleStreamingClientHttpRequest$NonClosingOutputStream.write(SimpleStreamingClientHttpRequest.java:123)
at org.springframework.util.FileCopyUtils.copy(FileCopyUtils.java:113)
at org.springframework.http.converter.ResourceHttpMessageConverter.writeInternal(ResourceHttpMessageConverter.java:87)
at org.springframework.http.converter.ResourceHttpMessageConverter.writeInternal(ResourceHttpMessageConverter.java:44)
at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:306)
at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:270)
at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:260)
at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:200)
at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:73)
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:657)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:482)
... 4 more