yI使用以下代码从internet下载文件。
private void download(String link){
URL url = new URL(link);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Encoding", "identity");
connection.setRequestProperty("connection", "close");
connection.connect();
save(connection.getInputStream());
}
private final int DOWNLOAD_BUFFER_SIZE = 10 * 1024;
private void save(InputStream inputStream) {
FileOutputStream fos;
try {
byte[] buffer = new byte[DOWNLOAD_BUFFER_SIZE];
int len;
fos = new FileOutputStream("FILE_PATH");
while ((len = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
一切正常,代码工作正常。但当我改变
DOWNLOAD_BUFFER_SIZE
对于较小的数字,如
5
,奇怪的事情发生了!当我更改
下载\u BUFFER\u大小
我关掉了我的互联网连接,下载会持续一段时间,不会停止!但当
下载\u BUFFER\u大小
大得像
10 * 1024
,一切正常,当我关闭连接时,下载停止。
有人能帮我吗?当
下载\u BUFFER\u大小
很小,我关闭了互联网连接。
我看到了
this
链接,但没有帮助。
EJP的答案不包含解决此问题的解决方案。由于某些原因,我希望DOWNLOAD\u BUFFER\u的大小较小。
请帮帮我。我需要你的帮助。请:X
如果我的句子语法不正确,我会提前道歉。因为我英语说得不好。