Browse Source

ResourceHttpMessageConverter does not call contentLength() on InputStreamResource

Issue: SPR-12017
pull/600/merge
Juergen Hoeller 12 years ago
parent
commit
f0bcb773f9
  1. 8
      spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,12 +18,12 @@ package org.springframework.http.converter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import javax.activation.FileTypeMap; import javax.activation.FileTypeMap;
import javax.activation.MimetypesFileTypeMap; import javax.activation.MimetypesFileTypeMap;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage; import org.springframework.http.HttpOutputMessage;
@ -78,7 +78,9 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
@Override @Override
protected Long getContentLength(Resource resource, MediaType contentType) throws IOException { 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 @Override

Loading…
Cancel
Save