Skip to content

Commit 30db112

Browse files
committed
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
1 parent 6fa4939 commit 30db112

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,17 @@ protected Long getContentLength(Resource resource, MediaType contentType) throws
8585
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
8686
throws IOException, HttpMessageNotWritableException {
8787

88-
StreamUtils.copy(resource.getInputStream(), outputMessage.getBody());
88+
InputStream in = resource.getInputStream();
89+
try {
90+
StreamUtils.copy(in, outputMessage.getBody());
91+
}
92+
finally {
93+
try {
94+
in.close();
95+
}
96+
catch (IOException ex) {
97+
}
98+
}
8999
outputMessage.getBody().flush();
90100
}
91101

0 commit comments

Comments
 (0)