Browse Source

ResourceHttpMessageConverter does not call contentLength() on InputStreamResource

Issue: SPR-12017
(cherry picked from commit f0bcb77)
pull/618/head
Juergen Hoeller 12 years ago
parent
commit
5342d92db7
  1. 10
      spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,12 +18,12 @@ package org.springframework.http.converter; @@ -18,12 +18,12 @@ package org.springframework.http.converter;
import java.io.IOException;
import java.io.InputStream;
import javax.activation.FileTypeMap;
import javax.activation.MimetypesFileTypeMap;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
@ -78,7 +78,9 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R @@ -78,7 +78,9 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
@Override
protected Long getContentLength(Resource resource, MediaType contentType) throws IOException {
return resource.contentLength();
// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
return (InputStreamResource.class.equals(resource.getClass()) ? null : resource.contentLength());
}
@Override
@ -138,7 +140,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R @@ -138,7 +140,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
}
public static MediaType getMediaType(Resource resource) {
if(resource.getFilename() == null) {
if (resource.getFilename() == null) {
return null;
}
String mediaType = fileTypeMap.getContentType(resource.getFilename());

Loading…
Cancel
Save