我試圖在我的下載管理器中實現暫停/恢復,我在網上搜索並閱讀了幾篇文章,並根據它們更改我的代碼,但恢復似乎無法正常工作,任何想法?在文件下載中執行暫停/恢復
if (!downloadPath.exists())
downloadPath.mkdirs();
if (outputFileCache.exists())
{
downloadedSize = outputFileCache.length();
connection.setAllowUserInteraction(true);
connection.setRequestProperty("Range", "bytes=" + downloadedSize + "-");
connection.setConnectTimeout(14000);
connection.connect();
input = new BufferedInputStream(connection.getInputStream());
output = new FileOutputStream(outputFileCache, true);
input.skip(downloadedSize); //Skip downloaded size
}
else
{
connection.setConnectTimeout(14000);
connection.connect();
input = new BufferedInputStream(url.openStream());
output = new FileOutputStream(outputFileCache);
}
fileLength = connection.getContentLength();
byte data[] = new byte[1024];
int count = 0;
int __progress = 0;
long total = downloadedSize;
while ((count = input.read(data)) != -1 && !this.isInterrupted())
{
total += count;
output.write(data, 0, count);
__progress = (int) (total * 100/fileLength);
}
output.flush();
output.close();
input.close();
究竟是不是工作? – BrianPlummer 2013-03-11 21:47:58
@BrianPlummer當我暫停並繼續下載時,下載從開始開始,並在到達最後狀態時被鎖定,我暫停它。 – NullPointer 2013-03-11 21:52:38
@BrianPlummer我覺得這行'(int)(total * 100/fileLength)'返回錯誤的進度! – NullPointer 2013-03-14 15:50:54