gpt4 book ai didi

java - 如何关闭已建立的网络连接?

转载 作者:行者123 更新时间:2023-12-01 18:48:10 26 4
gpt4 key购买 nike

我必须分部分下载文件。为此,我使用“xxx.setRequestProperty()”。但我收到错误消息说建立连接时无法使用它。所以我想关闭已经使用下载 URL 建立的连接。

try {

URL url = new URL(aurl[0]);
URLConnection connection = url.openConnection();

InputStream input = new BufferedInputStream(url.openStream());
Log.d(TAG,"connected");
int length = connection.getContentLength();

现在我想关闭“连接”。请建议一些方法来做到这一点。

最佳答案

您需要在此处的 try 语句后添加一个 finally block 并调用

input.close();
connection.disconnect();

在里面。您希望在finally block 中调用它们,以确保无论前面的代码是否失败,它们都会被调用。

关于java - 如何关闭已建立的网络连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16805946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com