|
1 | 1 | /* |
2 | | - * Copyright 2002-2013 the original author or authors. |
| 2 | + * Copyright 2002-2014 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
18 | 18 |
|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.io.InputStream; |
21 | | - |
22 | 21 | import javax.activation.FileTypeMap; |
23 | 22 | import javax.activation.MimetypesFileTypeMap; |
24 | 23 |
|
25 | 24 | import org.springframework.core.io.ByteArrayResource; |
26 | 25 | import org.springframework.core.io.ClassPathResource; |
| 26 | +import org.springframework.core.io.InputStreamResource; |
27 | 27 | import org.springframework.core.io.Resource; |
28 | 28 | import org.springframework.http.HttpInputMessage; |
29 | 29 | import org.springframework.http.HttpOutputMessage; |
@@ -78,7 +78,9 @@ protected MediaType getDefaultContentType(Resource resource) { |
78 | 78 |
|
79 | 79 | @Override |
80 | 80 | 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()); |
82 | 84 | } |
83 | 85 |
|
84 | 86 | @Override |
@@ -138,7 +140,7 @@ private static FileTypeMap loadFileTypeMapFromContextSupportModule() { |
138 | 140 | } |
139 | 141 |
|
140 | 142 | public static MediaType getMediaType(Resource resource) { |
141 | | - if(resource.getFilename() == null) { |
| 143 | + if (resource.getFilename() == null) { |
142 | 144 | return null; |
143 | 145 | } |
144 | 146 | String mediaType = fileTypeMap.getContentType(resource.getFilename()); |
|
0 commit comments