java 是否有必要关闭从 HttpServletRequest 返回的输入流?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1808248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:04:36  来源:igfitidea点击:

Is is necessary to close the input stream returned from HttpServletRequest?

javaservlets

提问by Conor

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

我有一些生产代码可以执行以下操作:

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

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?

这些流从不明确关闭。我在这里假设 servlet 容器管理这个(JBOss Web)。处理这个问题的正确方法是什么?

回答by BalusC

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.

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

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.

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

回答by skaffman

You should absolutely notclose these streams yourself, that is the container's job. Doing so manually risks interfering with the request lifecycle, and some containers may object violently to you doing this.

您绝对应该自己关闭这些流,这是容器的工作。手动这样做有干扰请求生命周期的风险,并且一些容器可能会强烈反对您这样做。

回答by Thomas Jung

The container will handle this. It is always good coding style to close resource in the same place you allocated it.(I was wrong on that in my original post. I thought you opened the stream. Should read more carefully.)

容器会处理这个。在分配资源的同一位置关闭资源总是好的编码风格。(我在原来的帖子中错了。我以为你打开了信息流。应该仔细阅读。)

回答by McDowell

The specification (up to the 3.0 candidate) does not say (as far as I can tell). In the absence of canonical information, you might be at the mercy of the implementation.

规范(直到 3.0 候选)没有说明(据我所知)。在没有规范信息的情况下,您可能会受制于实现。

The source code for the reference implementation is mentioned on the Sun Servlet page:

Sun Servlet 页面上提到了参考实现的源代码:

The reference implementation is included in the Java EE 5 SDK and also in the open-source Java Platform, Enterprise Edition (Java EE) application server, available through the GlassFish project, on java.net. The reference implementation source code for Servlet technology is available from the svn repository on java.net. Additional information on all webtier technologies in GlassFish can be found at the GlassFish Webtier page.

参考实现包含在 Java EE 5 SDK 和开源 Java 平台企业版 (Java EE) 应用程序服务器中,可通过 java.net 上的 GlassFish 项目获得。Servlet 技术的参考实现源代码可从 java.net 上的 svn 存储库获得。可以在 GlassFish Webtier 页面上找到有关 GlassFish 中所有 Webtier 技术的其他信息。

Checking the behaviour may be as close to a definitive answer as you'll get.

检查行为可能与您得到的确定答案一样接近。