如何在 Spring MVC 中处理 IO 流

How to handle IO streams in Spring MVC

我有一个方法 returns ResponseEntity(InputStreamResource)。在此方法中,我根据文件名输入从文件中获取 InputStream,然后将 InputStreamResource 作为响应发送。

代码片段

InputStream inputStream = ...;    
ResponseEntity<InputStreamResource> response = new ResponseEntity<InputStreamResource>(new InputStreamResource(inputStream), headers, HttpStatus.OK);
return response;

这里需要关闭inputStream对象吗?如果我这样做,我会得到

IllegalStateException: Closed message. Do I need to explicitly close them or container will take care.

处理此问题的基础 class 是 ResourceHttpMessageConverter and it closes the underlying input stream as is evident from here