【发布时间】:2019-04-01 12:49:22
【问题描述】:
我之前在this link 中问过类似的问题,但我仍然有这个问题。这是解释我的问题的代码:
package main;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainClass {
public static void main(String[] args) throws IOException {
URL url = new URL("http://speedtest-ny.turnkeyinternet.net/100mb.bin");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(10000);
con.connect();
InputStream inputStream = con.getInputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = inputStream.read(buffer)) != -1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("downlaoding : " + len);
}
System.out.println("finished");
}
}
如果我在下载过程中断开笔记本电脑与互联网的连接,下载不会结束并会持续一段时间(取决于我将笔记本电脑与互联网断开连接的时间)。我的问题是:
- 为什么会这样?
- 我该如何解决这个问题?
请帮助我。我真的需要你的回答。
如果我的句子语法不正确,我提前道歉。因为我英语说得不好。
【问题讨论】:
-
鉴于您处理数据的速度有多慢(1 kB/s),我怀疑数据已经下载并且只是缓冲等待您的进程处理它。您可以使用网络监控工具(如 Wireshark)来确认这一点。
-
@JoeC 你的意思是这段代码不会限制下载速度到 1 Kb/s 吗?
-
没错。
标签: java android download iostream