简体   繁体   English

Java 用 try 语句关闭 Connection 和 InputStream

[英]Java close both Connection and InputStream with try statement

Should I close HttpUrlConnection and InputStream in this case?在这种情况下我应该关闭 HttpUrlConnection 和 InputStream 吗? Only closing the connection will close the stream also?只有关闭连接才会关闭 stream 吗? I feel that it's a bad practice but don't know exactly why.我觉得这是一种不好的做法,但不知道为什么。

Closing both:关闭两者:

HttpURLConnection con = (HttpURLConnection) obj.openConnection();
try (AutoCloseable ac = con::disconnect) {
    int responseCode = con.getResponseCode();
    try (InputStream ins = responseCode >= 400 ? con.getErrorStream() : con.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(ins))) {
        // receive response
    }
}

Closing Connection only:仅关闭连接:

HttpURLConnection con = (HttpURLConnection) obj.openConnection();
try (AutoCloseable ac = con::disconnect) {
    int responseCode = con.getResponseCode();
    BufferedReader in = new BufferedReader(new InputStreamReader(ins)))
    // ins will close automatically when con closes?
    // receive response
}

When you disconnect the HttpURLConnection object, it MAY also close any InputStream or OutputStream that it has opened.当您断开HttpURLConnection object 时,它可能还会关闭它已打开的任何InputStreamOutputStream

HttpURLConnection.disconnect() method description: HttpURLConnection.disconnect()方法说明:

Calling the disconnect() method may close the underlying socket if a persistent connection is otherwise idle at that time.如果此时持久连接处于空闲状态,则调用 disconnect() 方法可能会关闭底层套接字。

You can read more here .您可以在此处阅读更多内容。

In turn, Socket.close() method description:反过来, Socket.close()方法说明:

Closing this socket will also close the socket's InputStream and OutputStream.关闭此套接字也会关闭套接字的 InputStream 和 OutputStream。 If this socket has an associated channel then the channel is closed as well.如果此套接字有关联的通道,则该通道也会关闭。

You can read more here .您可以在此处阅读更多内容。

But pay attention that "disconnecting" HttpURLConnection doesn't mandatory close the Socket .但请注意,“断开连接” HttpURLConnection并不强制关闭Socket It have been already discussed quite well in that thread :已经在该线程中讨论得很好:

(When keepAlive == true ) If client called HttpURLConnection.getInputSteam().close() , the later call to HttpURLConnection.disconnect() will NOT close the Socket . (当keepAlive == true时)如果客户端调用HttpURLConnection.getInputSteam().close() ,稍后调用HttpURLConnection.disconnect()不会关闭Socket ie The Socket is reused (cached) If client does not call close() , call disconnect() will close the InputStream and close the Socket .Socket被重用(缓存) 如果客户端不调用close() ,调用disconnect()将关闭InputStream并关闭Socket So in order to reuse the Socket , just call InputStream.close() .所以为了重用Socket ,只需调用InputStream.close() Do not call HttpURLConnection.disconnect() .不要调用HttpURLConnection.disconnect()

On the other hand, in the official oracle tutorials they suggest to close InputStream explicitly in order to be sure that resources don't leak.另一方面,在官方 oracle 教程中,他们建议明确关闭InputStream以确保资源不会泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 语句上的try-with-resources是否关闭连接? - Does try-with-resources on a statement close the connection? 为输入流和错误流尝试资源 - Try with resource for both inputstream and errorstream 我得到一个数据库被锁定的异常,我尝试关闭连接和语句,但这是 try 块中无法访问语句的问题 - I get an exception that the database is locked and I try to close connection and statement but here is the problem unreachable statement in try block 如何在Java中关闭读取inputStream - How to close reading inputStream in java 我是否需要同时调用.dispose()(javax.jcr.Binary)和.close()(java.io.InputStream)? - Do I need to call both .dispose() (javax.jcr.Binary) and .close() (java.io.InputStream)? Java-当我尝试关闭数据库资源时无法访问的语句 - Java - unreachable statement when i try to close database resources Try-with-resources - 它会自动关闭连接吗? Java - Try-with-resources - Does it automatically close the connection? Java 最终关闭连接并声明 - Close connection and statement finally 这会尝试使用资源关闭我的连接吗? - Will this try with resources close my Connection? Java inputStream作为对象属性,关闭HttpURLConnection - Java inputStream as object property, close HttpURLConnection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM