使用OkHttp實現下載的進度監聽和斷點續傳
1. 匯入依賴包
// retrofit, 基於Okhttp,考慮到專案中經常會用到retrofit,就匯入這個了。
compile 'com.squareup.retrofit2:retrofit:2.1.0'
// ButterKnife
compile 'com.jakewharton:butterknife:7.0.1'
// rxjava 本例中執行緒切換要用到,代替handler
compile 'io.reactivex:rxjava:1.1.6'
compile 'io.reactivex:rxandroid:1.2.1'
2. 繼承ResponseBody,生成帶進度監聽的ProgressResponseBody
// 參考okhttp的官方demo,此類當中我們主要把注意力放在ProgressListener和read方法中。在這裡獲取檔案總長我寫在了構造方法裡,這樣免得在source的read方法中重複呼叫或判斷。讀者也可以根據個人需要定製自己的監聽器。
public class ProgressResponseBody extends ResponseBody {
public interface ProgressListener {
void onPreExecute(long contentLength);
void update(long totalBytes, boolean done);
}
private final ResponseBody responseBody;
private final ProgressListener progressListener;
private BufferedSource bufferedSource;
public ProgressResponseBody(ResponseBody responseBody,
ProgressListener progressListener) {
this.responseBody = responseBody;
this.progressListener = progressListener;
if(progressListener!=null){
progressListener.onPreExecute(contentLength());
}
}
@Override
public MediaType contentType() {
return responseBody.contentType();
}
@Override
public long contentLength() {
return responseBody.contentLength();
}
@Override
public BufferedSource source() {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(source(responseBody.source()));
}
return bufferedSource;
}
private Source source(Source source) {
return new ForwardingSource(source) {
long totalBytes = 0L;
@Override
public long read(Buffer sink, long byteCount) throws IOException {
long bytesRead = super.read(sink, byteCount);
// read() returns the number of bytes read, or -1 if this source is exhausted.
totalBytes += bytesRead != -1 ? bytesRead : 0;
if (null != progressListener) {
progressListener.update(totalBytes, bytesRead == -1);
}
return bytesRead;
}
};
}
}
3.建立ProgressDownloader
//帶進度監聽功能的輔助類
public class ProgressDownloader {
public static final String TAG = "ProgressDownloader";
private ProgressListener progressListener;
private String url;
private OkHttpClient client;
private File destination;
private Call call;
public ProgressDownloader(String url, File destination, ProgressListener progressListener) {
this.url = url;
this.destination = destination;
this.progressListener = progressListener;
//在下載、暫停後的繼續下載中可複用同一個client物件
client = getProgressClient();
}
//每次下載需要新建新的Call物件
private Call newCall(long startPoints) {
Request request = new Request.Builder()
.url(url)
.header("RANGE", "bytes=" + startPoints + "-")//斷點續傳要用到的,指示下載的區間
.build();
return client.newCall(request);
}
public OkHttpClient getProgressClient() {
// 攔截器,用上ProgressResponseBody
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.body(new ProgressResponseBody(originalResponse.body(), progressListener))
.build();
}
};
return new OkHttpClient.Builder()
.addNetworkInterceptor(interceptor)
.build();
}
// startsPoint指定開始下載的點
public void download(final long startsPoint) {
call = newCall(startsPoint);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
save(response, startsPoint);
}
});
}
public void pause() {
if(call!=null){
call.cancel();
}
}
private void save(Response response, long startsPoint) {
ResponseBody body = response.body();
InputStream in = body.byteStream();
FileChannel channelOut = null;
// 隨機訪問檔案,可以指定斷點續傳的起始位置
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(destination, "rwd");
//Chanel NIO中的用法,由於RandomAccessFile沒有使用快取策略,直接使用會使得下載速度變慢,親測快取下載3.3秒的檔案,用普通的RandomAccessFile需要20多秒。
channelOut = randomAccessFile.getChannel();
// 記憶體對映,直接使用RandomAccessFile,是用其seek方法指定下載的起始位置,使用快取下載,在這裡指定下載位置。
MappedByteBuffer mappedBuffer = channelOut.map(FileChannel.MapMode.READ_WRITE, startsPoint, body.contentLength());
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
mappedBuffer.put(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in.close();
if (channelOut != null) {
channelOut.close();
}
if (randomAccessFile != null) {
randomAccessFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4. 測試demo
清單檔案中新增網路許可權和檔案訪問許可權
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
MainActivity
public class MainActivity extends AppCompatActivity implements ProgressResponseBody.ProgressListener {
public static final String TAG = "MainActivity";
public static final String PACKAGE_URL = "http://gdown.baidu.com/data/wisegame/df65a597122796a4/weixin_821.apk";
@Bind(R.id.progressBar)
ProgressBar progressBar;
private long breakPoints;
private ProgressDownloader downloader;
private File file;
private long totalBytes;
private long contentLength;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick({R.id.downloadButton, R.id.cancel_button, R.id.continue_button})
public void onClick(View view) {
switch (view.getId()) {
case R.id.downloadButton:
// 新下載前清空斷點資訊
breakPoints = 0L;
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "sample.apk");
downloader = new ProgressDownloader(PACKAGE_URL, file, this);
downloader.download(0L);
break;
case R.id.pause_button:
downloader.pause();
Toast.makeText(this, "下載暫停", Toast.LENGTH_SHORT).show();
// 儲存此時的totalBytes,即斷點位置。
breakPoints = totalBytes;
break;
case R.id.continue_button:
downloader.download(breakPoints);
break;
}
}
@Override
public void onPreExecute(long contentLength) {
// 檔案總長只需記錄一次,要注意斷點續傳後的contentLength只是剩餘部分的長度
if (this.contentLength == 0L) {
this.contentLength = contentLength;
progressBar.setMax((int) (contentLength / 1024));
}
}
@Override
public void update(long totalBytes, boolean done) {
// 注意加上斷點的長度
this.totalBytes = totalBytes + breakPoints;
progressBar.setProgress((int) (totalBytes + breakPoints) / 1024);
if (done) {
// 切換到主執行緒
Observable
.empty()
.observeOn(AndroidSchedulers.mainThread())
.doOnCompleted(new Action0() {
@Override
public void call() {
Toast.makeText(MainActivity.this, "下載完成", Toast.LENGTH_SHORT).show();
}
})
.subscribe();
}
}
}
最後是動態效果圖
相關文章
- Android okhttp+rxjava實現多檔案下載和斷點續傳AndroidHTTPRxJava斷點
- 一行程式碼實現Okhttp,Retrofit,Glide下載上傳進度監聽行程HTTPIDE
- OkHttp優雅的實現下載監聽HTTP
- Android OkHttp+RxJava 史上最優雅的實現檔案上傳/下載進度的監聽AndroidHTTPRxJava
- Android下載檔案(一)下載進度&斷點續傳Android斷點
- OkHttp使用+檔案的上傳+斷點續傳HTTP斷點
- flutter使用dio實現 檔案下載並實現進度監聽總結Flutter
- 使用curl斷點續傳下載檔案斷點
- PHP實現檔案下載斷點續傳詳解PHP斷點
- vue/js實現檔案流下載,檔案下載進度監聽VueJS
- Android 史上最優雅的實現檔案上傳、下載及進度的監聽Android
- jQuery監聽檔案上傳實現進度條效果jQuery
- iOS 開發之 NSURLSession 下載和斷點續傳iOSSession斷點
- 使用Visual C#實現斷點續傳C#斷點
- scp實現斷點續傳---rsync斷點
- Winform檔案下載之斷點續傳ORM斷點
- C# FTP上傳下載(支援斷點續傳)C#FTP斷點
- C# 上傳下載ftp(支援斷點續傳)C#FTP斷點
- Git斷點續傳和離線增量更新的實現Git斷點
- iOS11 下載之斷點續傳的bugiOS斷點
- php 支援斷點續傳的檔案下載類PHP斷點
- requests如何友好地請求下載大檔案?requests實現分段下載、斷點續傳斷點
- Android多執行緒+單執行緒+斷點續傳+進度條顯示下載Android執行緒斷點
- 用Java實現斷點續傳(HTTP)Java斷點HTTP
- Java實現檔案斷點續傳Java斷點
- iOS 下載URL不斷改變的情況下 使用 resumeData做斷點續傳iOS斷點
- 檔案下載之斷點續傳(客戶端與服務端的實現)斷點客戶端服務端
- 利用HTTP協議實現檔案下載的多執行緒斷點續傳HTTP協議執行緒斷點
- C# 檔案下載之斷點續傳C#斷點
- vue+element+oss實現前端分片上傳和斷點續傳Vue前端斷點
- C# 斷點續傳原理與實現C#斷點
- Android 中 Service+Notification 斷點續傳下載Android斷點
- Android斷點續傳下載器JarvisDownloaderAndroid斷點JAR
- 使用webuploader元件實現大檔案分片上傳,斷點續傳Web元件斷點
- 使用Retrofit+RxJava實現帶進度下載RxJava
- C#如何使用HttpClient對大檔案進行斷點上傳和下載C#HTTPclient斷點
- JAVA實現大檔案分片上傳斷點續傳Java斷點
- 斷點續傳斷點