Close InputStream in ResourceHttpMessageConverter

Spring 3.2.2 introduced a change to avoid closing the response stream
in HttpMessageConverters (SPR-10095). However, the InputStream of
resources being written, for example as part of a multi-part request
should be closed. This change ensures that.

Issue: SPR-10460
This commit is contained in:
Rossen Stoyanchev 2013-05-10 17:26:35 -04:00
parent 6fa493908b
commit 30db112d37
1 changed files with 11 additions and 1 deletions

View File

@ -85,7 +85,17 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
StreamUtils.copy(resource.getInputStream(), outputMessage.getBody());
InputStream in = resource.getInputStream();
try {
StreamUtils.copy(in, outputMessage.getBody());
}
finally {
try {
in.close();
}
catch (IOException ex) {
}
}
outputMessage.getBody().flush();
}