如何在Android中使用okhttp暂停/恢复下载 [英] How to pause/resume downloading with okhttp in Android

查看:167
本文介绍了如何在Android中使用okhttp暂停/恢复下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用okhttp库在android中下载文件.我下载成功.但是当我暂停并恢复下载时出了点问题.

I use okhttp library for download files in android. I download successfully. But something is wrong when I pause and resume download.

Response request = new Request.Builder().url(url).build();
ResponseBody responseBody = response.body();

File file = new File(filePath);
BufferedInputStream input = new BufferedInputStream(responseBody.byteStream());
OutputStream output;

if (isResume) {
    output = new FileOutputStream(file, true);
    input.skip(downloadedSize);
} else {
    output = new FileOutputStream(file, false);    
}

long totalByteSize = responseBody.contentLength();
byte[] data = new byte[1024];
int count = 0;

while ((count = input.read(data)) != -1) {
    downloadedSize += count;
    output.write(data, 0, count);   
}

问题是例如文件大小为10MB.下载3MB时我暂停,然后恢复下载,下载完成后文件大小变为13MB.它不是从恢复时的下载大小开始的,而是从字节流的开头开始下载的.因此文件变为13MB.代码有什么问题?

The problem is that for example size of file is 10MB. I pause when it downloaded 3MB and then resume to download and when download is finish size of file become 13MB. It doesnt start from downloaded size on resume, it start download from begining of bytestream. so file become 13MB. What is wrong with code?

推荐答案

第一条路

我尝试了很多代码,最后我用 BufferedSource source = responseBody.source();解决了. source.skip(downloadedSize);

I tried a lot of codes and finally I solved with BufferedSource source = responseBody.source(); source.skip(downloadedSize);

Response request = new Request.Builder().url(url).build();
ResponseBody responseBody = response.body();
BufferedSource source = responseBody.source();

if(isResume)
    source.skip(downloadedSize);

File file = new File(filePath);
BufferedInputStream input = new BufferedInputStream(responseBody.byteStream());
OutputStream output;

if (isResume) {
    output = new FileOutputStream(file, true);
} else {
    output = new FileOutputStream(file, false);    
}

long currentDownloadedSize = 0;
long currentTotalByteSize = responseBody.contentLength();
byte[] data = new byte[1024];
int count = 0;
while ((count = input.read(data)) != -1) {
    currentDownloadedSize += count;
    output.write(data, 0, count);   
}

工作成功.我想我很幸运:)

It worked successfully. I think I'm lucky :)

第二种方式

我为跳过下载的字节添加了标头,并且有效.

I added header for skip downloaded bytes and it worked.

Request.Builder requestBuilder = new Request.Builder();
if (isResume) {
    requestBuilder.addHeader("Range", "bytes=" + String.valueOf(downloadedSize) + "-");
}
Response request = requestBuilder.url(url).build();
ResponseBody responseBody = response.body();
BufferedSource source = responseBody.source();

File file = new File(filePath);
BufferedInputStream input = new BufferedInputStream(responseBody.byteStream());
OutputStream output;

if (isResume) {
    output = new FileOutputStream(file, true);
} else {
    output = new FileOutputStream(file, false);    
}

long currentDownloadedSize = 0;
long currentTotalByteSize = responseBody.contentLength();
byte[] data = new byte[1024];
int count = 0;
while ((count = input.read(data)) != -1) {
    currentDownloadedSize += count;
    output.write(data, 0, count);   
}

这篇关于如何在Android中使用okhttp暂停/恢复下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆