如何在Spring MVC中处理IO流 [英] How to handle IO streams in Spring MVC
问题描述
我有一个返回 ResponseEntity(InputStreamResource)的方法.在这种方法中,我从基于文件名输入的文件中获取InputStream,然后发送InputStreamResource作为响应.
I have a method which returns ResponseEntity(InputStreamResource). In this method, I'm getting InputStream from a file based on the filename input and then sending InputStreamResource as a response.
代码段
InputStream inputStream = ...;
ResponseEntity<InputStreamResource> response = new ResponseEntity<InputStreamResource>(new InputStreamResource(inputStream), headers, HttpStatus.OK);
return response;
我需要关闭inputStream对象吗?如果这样做,我会得到
Here do I need to close inputStream object? If I do so, I'm getting
IllegalStateException:已关闭消息.我需要显式关闭它们,否则容器会小心.
IllegalStateException: Closed message. Do I need to explicitly close them or container will take care.
推荐答案
处理该问题的基础类是此处
The underlying class where this is handled is ResourceHttpMessageConverter and it closes the underlying input stream as is evident from here
这篇关于如何在Spring MVC中处理IO流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!