Skip to content

Commit f0bcb77

Browse files
committed
ResourceHttpMessageConverter does not call contentLength() on InputStreamResource
Issue: SPR-12017
1 parent c082220 commit f0bcb77

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

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

Lines changed: 5 additions & 3 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

0 commit comments

Comments
 (0)