如何在Spring MVC中处理IO流 [英] How to handle IO streams in Spring MVC

查看:231
本文介绍了如何在Spring MVC中处理IO流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆