Return a stream with Spring MVC's ResponseEntity

后端 未结 1 848
情话喂你
情话喂你 2020-12-02 13:04

I have a Spring MVC method which returns a ResponseEntity. Depending on the specific data retrieved, it sometimes needs to return a stream of data to t

相关标签:
1条回答
  • 2020-12-02 13:25

    Spring's InputStreamResource works well. You need to set the Content-Length manually, or it appears that Spring attempts to read the stream to obtain the 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.

    0 讨论(0)
提交回复
热议问题