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非常適合該文件。如何恢復文件下載?
3 回答

喵喵時光機
TA貢獻1846條經驗 獲得超7個贊
我猜你所面臨的問題是調用url.openStream()后url.openConnection()。
url.openStream()等同于url.openConnection().getInputStream()。因此,您兩次請求該URL。特別是第二次,它沒有指定range屬性。因此,下載始終從頭開始。
您應該替換url.openStream()為connection.getInputStream()。

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
簽出該線程,它具有與您類似的問題。如果wget正常工作,則服務器顯然支持恢復下載。好像您沒有If-Range按照上面鏈接的可接受答案中所述設置標題。即。加:
// Initial download.
String lastModified = connection.getHeaderField("Last-Modified");
// ...
// Resume download.
connection.setRequestProperty("If-Range", lastModified);
添加回答
舉報
0/150
提交
取消