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);
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中我尝试恢复下载.目标文件是20MB.但是当我停止下载10mb,然后contunue时,我得到文件大小为30MB的文件.它似乎继续写入文件,但无法从服务器部分下载.Wget -c适合这个文件.如何恢复文件下载?
我正在尝试在我的下载管理器中实现暂停/恢复,我搜索网络并阅读几篇文章并根据它们更改我的代码,但恢复似乎无法正常工作,任何想法?
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, …Run Code Online (Sandbox Code Playgroud) 我希望在下载时实现这一点,它可以暂停,由用户停止.如何使用DownloadManager实现这一目标?
我希望能够在Android中打开与给定文件的http连接并开始下载.我还必须能够在某个时刻暂停下载并稍后恢复.这是如何在Android中实现的?我不想再重新开始下载.
我需要实现具有暂停和恢复机制的Download-Manager.
下载大型视频文件或任何其他类型需要此管理器.
我想过使用Android的DownloadManager,但据我所知,DownloadManager不支持用户手动暂停和恢复.
除了自己编写这个组件外,我还有哪些其他Android内置选项?或者也许有人知道如何使用DownloadManager手动暂停和恢复?