恢复 http 下载。文件已追加,但大小更大
URL url;
url = new URL("http://download.thinkbroadband.com/5MB.zip");
File fileThatExists = new File("/sdcard/testfile");
URLConnection conexion = url.openConnection();
conexion.setRequestProperty("Range", "bytes=" + fileThatExists.length() + "-");
// Resume download.
conexion.setRequestProperty("If-Range", "Mon, 02 Jun 2008 15:30:42 GMT");
conexion.connect();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/testfile", true);
byte data[] = new byte[1024];
long total = 0;
int i = 0;
while ((count = input.read(data)) != -1) {
total += count;
i++;
output.write(data, 0, count);
}
}
我尝试恢复下载。但是如果我的文件是 5200kb,并且我在 100kb 后恢复下载,我得到的文件是 5300kb。这段代码有什么问题?
URL url;
url = new URL("http://download.thinkbroadband.com/5MB.zip");
File fileThatExists = new File("/sdcard/testfile");
URLConnection conexion = url.openConnection();
conexion.setRequestProperty("Range", "bytes=" + fileThatExists.length() + "-");
// Resume download.
conexion.setRequestProperty("If-Range", "Mon, 02 Jun 2008 15:30:42 GMT");
conexion.connect();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/testfile", true);
byte data[] = new byte[1024];
long total = 0;
int i = 0;
while ((count = input.read(data)) != -1) {
total += count;
i++;
output.write(data, 0, count);
}
}
I tried to resume download. But If my file is 5200kb, and I resume download after 100kb, I got file 5300kb. What's wrong with this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论