Java 使用 Spring MVC 的 ResponseEntity 返回一个流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20333394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return a stream with Spring MVC's ResponseEntity
提问by David V
I have a Spring MVC method which returns a ResponseEntity
. Depending on the specific data retrieved, it sometimesneeds to return a stream of data to the user. Other times it will return something other than a stream, and sometimes a redirect. I most definitely want this to be a stream and not a byte array since it can be large.
我有一个 Spring MVC 方法,它返回一个ResponseEntity
. 根据检索到的具体数据,有时需要向用户返回数据流。其他时候它会返回一个流以外的东西,有时会返回一个重定向。我绝对希望它是一个流而不是一个字节数组,因为它可能很大。
Currently, I return the stream using the following snippet of code:
目前,我使用以下代码片段返回流:
HttpHeaders httpHeaders = createHttpHeaders();
IOUtils.copy(inputStream, httpServletResponse.getOutputStream());
return new ResponseEntity(httpHeaders, HttpStatus.OK);
Unfortunately, this does not allow the Spring HttpHeaders
data to actually populate the HTTP Headers in the response. This makes sense since my code writes to the OutputStream
before Spring receives the ResponseEntity
.
不幸的是,这不允许 SpringHttpHeaders
数据实际填充响应中的 HTTP 标头。这是有道理的,因为我的代码OutputStream
在 Spring 收到ResponseEntity
.
It would be very nice to somehow return a ResponseEntity
with an InputStream
an let Spring handle it. It also would parallel the other paths of my function where I can successfully return a ResponseEntity
. Is there anyway I can accomplish this with Spring?
以某种方式返回 aResponseEntity
并InputStream
让 Spring 处理它会非常好。它也将与我的函数的其他路径平行,在那里我可以成功地返回一个ResponseEntity
. 无论如何我可以用Spring完成这个吗?
Also, I did try returning the InputStream
in the ResponseEntity
just to see if Spring would accept it.
此外,我确实尝试返回InputStream
inResponseEntity
只是为了看看 Spring 是否会接受它。
return new ResponseEntity(inputStream, httpHeaders, HttpStatus.OK);
But it throws this exception:
但它抛出这个异常:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
I can get my function to work by setting everything on the HttpServletResponse
directly, but I would like to do this only with Spring.
我可以通过HttpServletResponse
直接在 上设置所有内容来使我的函数工作,但我只想用 Spring 来做到这一点。
采纳答案by David V
Spring's InputStreamResourceworks well. You need to set the Content-Length manually, or it appears that Spring attempts to read the stream to obtain the Content-Length.
Spring 的InputStreamResource运行良好。您需要手动设置 Content-Length,否则 Spring 会尝试读取流以获取 Content-Length。
InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
httpHeaders.setContentLength(contentLengthOfStream);
return new ResponseEntity(inputStreamResource, httpHeaders, HttpStatus.OK);
I never found any web pages suggesting using this class. I only guessed it because I noticed there were a few suggestions for using ByteArrayResource.
我从未发现任何建议使用此类的网页。我只是猜测,因为我注意到有一些关于使用 ByteArrayResource 的建议。