关闭从HttpServletRequest返回的输入流是否必要? [英] Is is necessary to close the input stream returned from HttpServletRequest?

查看:45
本文介绍了关闭从HttpServletRequest返回的输入流是否必要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些生产代码,其功能类似于:

I've got some production code that does something like:

HttpServletRequest httpServletRequest
...
DataInputStream dis = new DataInputStream(httpServletRequest.getInputStream())

这些流永远不会显式关闭.我在这里假设servlet容器对此进行管理(JBOss Web).处理此问题的正确方法是什么?

These streams are never closed explicitly. I'm assuming here that the servlet container manages this (JBOss Web). What is the correct way to handle this?

推荐答案

I/O中的经验法则是,如果您没有自己打开/创建输入流源,则不一定也需要关闭它.在这里,您只是包装请求的输入流,因此不必关闭它.

The thumb rule in I/O is, if you did not open/create the inputstream source yourself, then you do not necessarily need to close it as well. Here you are just wrapping the request's inputstream, so you don't necessarily need to close it.

如果您确实通过以下方式自己打开了输入: new FileInputStream("c:/file.ext"),那么您显然需要自己在finally块中将其关闭.容器应该在引擎盖下这样做.

If you did open the input yourself by e.g. new FileInputStream("c:/file.ext") then you obviously need to close it yourself in the finally block. The container ought to do so under the hood.

这篇关于关闭从HttpServletRequest返回的输入流是否必要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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