在 java 中恢复 http 文件下载
URL url = new URL("http://download.thinkbroadband.com/20MB.zip");
URLConnection connection = url.openConnection();
File fileThatExists = new File(path);
OutputStream output = new FileOutputStream(path, true);
connection.setRequestProperty("Range", "bytes=" + fileThatExists.length() + "-");
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0 , count);
}
在此代码中,我尝试恢复下载。目标文件为 20MB。但是当我在10mb上停止下载,然后协调时,我得到文件大小为30MB的文件。它似乎继续写入文件,但不能部分从服务器下载。Wget -c 非常适合此文件。如何恢复文件下载?