下载文件超时
我正在使用这种方式下载文件,
URL url = new URL(URL)
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setConnectTimeout(TimeOut);
connection.setReadTimeout(TimeOut);
connection.connect();
String status = connection.getHeaderField(0);
我想要做的是,如果文件没有在指定的时间内下载,那么 它会停止下载或给出超时异常。
我有 setConnectTimeout()
但仅在未建立连接时给出异常 在初始连接时间内。
I am downloading a file using this way
URL url = new URL(URL)
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setConnectTimeout(TimeOut);
connection.setReadTimeout(TimeOut);
connection.connect();
String status = connection.getHeaderField(0);
what I want do is if the file is not downloaded in the specified time then
it stops download or give timeout Exception.
I had setConnectTimeout()
but only give exception if the connection is not established
within the initial connection time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有尝试过这个,但我想你可以将布尔变量设置为“finished = false;”在开始下载之前。
然后将其设置为“finished = true;”下载完成后。
然后,创建一个线程,定期或在所需时间后检查此变量。如果布尔值不具有所需的状态,则中止下载(断开连接,关闭流)。
另请查看 AsyncTask 类。
I have not tried this, but I guess you could set a boolean variable to 'finished = false;' before you start downloading.
Then set this to 'finished = true;' when download is finished.
Then, create a thread that checks this variable on a regular interval or after the desired time. If the boolean value does not have the desired state, abort the download (disconnect connection, close the stream).
Also have a look at the AsyncTask-class.