Streaming large files with spring mvc

谁说胖子不能爱 提交于 2019-12-10 11:53:22

问题


I'm trying to create an application that download and uploads large files, so I don't want the file contents to be stored in memory.

On the mvc controller side I'm using an http message converter that converts to / from InputStream

@Override
public InputStream read(Class<? extends InputStream> clazz, HttpInputMessage inputMessage) throws IOException,
        HttpMessageNotReadableException {
    return inputMessage.getBody();
}

@Override
public void write(InputStream t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException,
        HttpMessageNotWritableException {

    try {
        IOUtils.copy(t, outputMessage.getBody());
    } finally {
        IOUtils.closeQuietly(t);
    }
}

This works well on the server side.

On the client (RestTemplate) side I tried to use the same converter, but I got an exception that the stream has been closed (probably closed when the request was completed).

Client side code:

ResponseEntity<InputStream> res = rest.getForEntity(url, InputStream.class);
// res.getBody() is closed

I've also tried to copy the input stream into a buffer and create a new ByteArrayInputStream and return it to the RestTemplate client and it worked well, however it does require that the data will be read into memory which doesn't suite my demands.

My question is how to keep the stream open until I process it without having to read it all into memory / file?

Any idea will be appreciated.

Regards, Shay


回答1:


As far as I am aware, RestTemplate's getForEntity() is not an appropriate way to get an InputStream. It's a convenience for converting to and from entity classes, so presumably that's where your problem lies.

Since you are used to HttpInputMessage, why don't you use HttpInputMessage.getBody() on the client side as well? It gets you a nice InputStream, which would be ready for passing straight to an OutputStream such as HttpServletResponse.getOutputStream().




回答2:


Check how Spring MVC handles large files upload with org.springframework.web.multipart.commons.CommonsMultipartResolver. It has a 'maxInMemorySize' that can help control the memory requirements. See this thread for using a multipart resolver with the REST template Sending Multipart File as POST parameters with RestTemplate requests



来源:https://stackoverflow.com/questions/26711526/streaming-large-files-with-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!