Skip to content

Commit 5342d92

Browse files
committed
ResourceHttpMessageConverter does not call contentLength() on InputStreamResource
Issue: SPR-12017 (cherry picked from commit f0bcb77)
1 parent 7af1e00 commit 5342d92

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,12 +18,12 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21-
2221
import javax.activation.FileTypeMap;
2322
import javax.activation.MimetypesFileTypeMap;
2423

2524
import org.springframework.core.io.ByteArrayResource;
2625
import org.springframework.core.io.ClassPathResource;
26+
import org.springframework.core.io.InputStreamResource;
2727
import org.springframework.core.io.Resource;
2828
import org.springframework.http.HttpInputMessage;
2929
import org.springframework.http.HttpOutputMessage;
@@ -78,7 +78,9 @@ protected MediaType getDefaultContentType(Resource resource) {
7878

7979
@Override
8080
protected Long getContentLength(Resource resource, MediaType contentType) throws IOException {
81-
return resource.contentLength();
81+
// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
82+
// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
83+
return (InputStreamResource.class.equals(resource.getClass()) ? null : resource.contentLength());
8284
}
8385

8486
@Override
@@ -138,7 +140,7 @@ private static FileTypeMap loadFileTypeMapFromContextSupportModule() {
138140
}
139141

140142
public static MediaType getMediaType(Resource resource) {
141-
if(resource.getFilename() == null) {
143+
if (resource.getFilename() == null) {
142144
return null;
143145
}
144146
String mediaType = fileTypeMap.getContentType(resource.getFilename());

0 commit comments

Comments
 (0)