ResourceHttpMessageConverter reads Content-Disposition header to expose filename through Resource

Issue: SPR-15191
This commit is contained in:
Juergen Hoeller
2017-01-30 22:15:30 +01:00
parent f51b896bd9
commit 768802fa96
2 changed files with 27 additions and 11 deletions

View File

@@ -82,12 +82,23 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
final String filename = inputMessage.getHeaders().getContentDisposition().getFilename();
if (this.supportsReadStreaming && InputStreamResource.class == clazz) {
return new InputStreamResource(inputMessage.getBody());
return new InputStreamResource(inputMessage.getBody()) {
@Override
public String getFilename() {
return filename;
}
};
}
else if (clazz.isAssignableFrom(ByteArrayResource.class)) {
byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody());
return new ByteArrayResource(body);
return new ByteArrayResource(body) {
@Override
public String getFilename() {
return filename;
}
};
}
else {
throw new IllegalStateException("Unsupported resource class: " + clazz);