Java的HttpURLConnecction的getInputStream是否需要close?
迷茫
迷茫 2017-04-18 10:49:55
0
2
495

RT,就酱
Java的HttpURLConnecction的getInputStream是否需要close?

这个流是否是系统维护的?
因为不是我打开的

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
巴扎黑

Get into the habit of closing all InputStreams and OutputStreams that are no longer in use, regardless of whether the implementation behind the API automatically closes them for you.
If written in Java 1.7+, it is very simple to use try-with-resource:

try (InputStream in = urlConnection.getInputStream()) {
    ...
} // 这里会自动调用 in.close();

Similar scenes include:

  • JDBC’s PreparedStatement, ResultSet, etc. all implement the AutoCloseable interface. In theory, as long as the Connection is closed, the PreparedStatement, ResultSet, etc. generated by it will also be automatically closed. However, it is strongly recommended that you use try-with when generating these resources. -resource to manage.

  • The same goes for resources such as getInputStream/getOutputStream in ServletRequest/ServletResponse.

洪涛

Need

In the case of very high concurrent operations, if you do not close it, the overhead on system resources will be relatively large.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template