【发布时间】:2013-03-11 21:45:52
【问题描述】:
我正在尝试在我的下载管理器中实现暂停/恢复,我在网上搜索并阅读了几篇文章并根据它们更改了我的代码,但恢复似乎无法正常工作,有什么想法吗?
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 当我暂停并继续下载时,下载从头开始,并在到达我暂停的最后状态时锁定。
-
@BrianPlummer 我认为这条线
(int) (total * 100 / fileLength)返回错误的进度! -
@DKN 为什么你使用
input.skip(downloadedSize);,这不是从服务器端完成的吗? -
input.skip(downloadedSize);的结果真的等于downloadedSize吗?