You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RestTemplate with InputStreamResource does not work if Content-Length is not set [SPR-12017] #16633
When using an InputStreamResource to upload a file for example
...
MultiValueMap<String, Object> postData = new LinkedMultiValueMap<>();
postData.add("param", "something");
postData.add("file", new InputStreamResource(in));
restTemplate.postForObject(url, postData, String.class);
This causes a strange exception to be thrown: IllegalStateException: InputStream has already been read....
Looking through the code it appears that what is occurring is when AbstractResource.contentLength() is called, it actually reads the entire InputStream. This makes it impossible to use InputStreamResource without defining the Content-Length header. It would be nice if this were not the case. Or if this is required for some reason, at least it should be documented so other people don't have to waste time debugging like I had to.
The problem here is that with an InputStreamResource, the given InputStream can technically only be read once. It's unfortunate here that AbstractResource's contentLength() implementation performs that one read attempt and therefore renders the InputStreamResource unusable afterwards. I guess we should rather throw an IllegalStateException from an InputStreamResource-specific contentLength() implementation, signalling that the content length cannot be determined without side effects. RestTemplate and other callers can deal with that accordingly then, simply not knowing the content length in advance.
That said, it's definitely recommendable to use one of the more specific Resource implementations if you can, which among other benefits are usually capable of determining the content length without side effects. InputStreamResource should be a last resort only.
Alternatively, we could also let RestTemplate check for an InputStreamResource and simply not call contentLength() on it in that case, since it knows that the InputStreamResource will be used for full content reading afterwards.
For other kinds of callers, just determining the contentLenght() might be alright if they never end up reading the actual content afterwards.
Activity
spring-projects-issues commentedon Jul 22, 2014
Juergen Hoeller commented
The problem here is that with an InputStreamResource, the given InputStream can technically only be read once. It's unfortunate here that AbstractResource's contentLength() implementation performs that one read attempt and therefore renders the InputStreamResource unusable afterwards. I guess we should rather throw an IllegalStateException from an InputStreamResource-specific contentLength() implementation, signalling that the content length cannot be determined without side effects. RestTemplate and other callers can deal with that accordingly then, simply not knowing the content length in advance.
That said, it's definitely recommendable to use one of the more specific Resource implementations if you can, which among other benefits are usually capable of determining the content length without side effects. InputStreamResource should be a last resort only.
Juergen
spring-projects-issues commentedon Jul 22, 2014
Juergen Hoeller commented
Alternatively, we could also let RestTemplate check for an InputStreamResource and simply not call contentLength() on it in that case, since it knows that the InputStreamResource will be used for full content reading afterwards.
For other kinds of callers, just determining the contentLenght() might be alright if they never end up reading the actual content afterwards.
Juergen
spring-projects-issues commentedon Jul 22, 2014
Juergen Hoeller commented
As of 4.1 RC2, ResourceHttpMessageConverter does not call contentLength() on InputStreamResource anymore. To be backported to 4.0.7 and 3.2.11.
Juergen