什么是断点续传
FTP(文件传输协议的简称)(File Transfer Protocol、 FTP)客户端软件断点续传指的是在下载或上传时,将下载或上传任务(一个文件或一个压缩包)人为的划分为几个部分,每一个部分采用一个线程进行上传或下载,如果碰到网络故障,可以从已经上传或下载的部分开始继续上传下载未完成的部分,而没有必要从头开始上传下载。用户可以节省时间,提高速度。
有时用户上传下载文件需要历时数小时,万一线路中断,不具备断点续传的FTP服务器或下载软件就只能从头重传,比较好的FTP服务器或下载软件具有FTP断点续传能力,允许用户从上传下载断线的地方继续传送,这样大大减少了用户的烦恼。
<1> 配置相关权限
<!-- 存储卡 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 网络 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<2> 下载进度接口
package com.wjn.okhttpmvpdemo.mode.breakpointcontinuingly; /** * 下载进度 */ public interface ProgressListener { void onPreExecute(long contentLength); void update(long totalBytes, boolean done); }
<3>ResponseBody继承类
package com.wjn.okhttpmvpdemo.mode.breakpointcontinuingly; import java.io.IOException; import okhttp3.MediaType; import okhttp3.ResponseBody; import okio.Buffer; import okio.BufferedSource; import okio.ForwardingSource; import okio.Okio; import okio.Source; /** * ResponseBody继承类 */ public class ProgressResponseBody extends ResponseBody { 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; } /** * 获取Source */ 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; } }; } }
<4> OkHttp拦截器
package com.wjn.okhttpmvpdemo.mode.breakpointcontinuingly; import java.io.IOException; import okhttp3.Interceptor; import okhttp3.Response; /** * OkHttp拦截器 */ public class DownloaderInterceptor implements Interceptor { private ProgressListener progressListener; public DownloaderInterceptor(ProgressListener progressListener) { this.progressListener = progressListener; } @Override public Response intercept(Chain chain) throws IOException { if (null == chain || null == progressListener) { return null; } Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .body(new ProgressResponseBody(originalResponse.body(), progressListener)) .build(); } }
<5> OkHttpClient操作下载类
package com.wjn.okhttpmvpdemo.mode.breakpointcontinuingly; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; public class 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 = getProgressClient();//获取OkHttpClient对象 } /** * 获取OkHttpClient对象 */ public OkHttpClient getProgressClient() { return new OkHttpClient.Builder() .addNetworkInterceptor(new DownloaderInterceptor(progressListener))//拦截器 .build(); } /** * 下载 * * @param 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); } }); } /** * newCall方法 */ private Call newCall(long startPoints) { Request request = new Request.Builder() .url(url) .header("RANGE", "bytes=" + startPoints + "-")//断点续传要用到的,指示下载的区间 .build(); return client.newCall(request); } /** * 保存文件 */ 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(); } } } /** * 暂停 */ public void pause() { if (call != null) { call.cancel(); } } }
<6> Activity测试
package com.wjn.okhttpmvpdemo.view.impl.activity; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.Toast; import com.wjn.okhttpmvpdemo.R; import com.wjn.okhttpmvpdemo.mode.breakpointcontinuingly.ProgressDownloader; import com.wjn.okhttpmvpdemo.mode.breakpointcontinuingly.ProgressListener; import com.wjn.okhttpmvpdemo.mode.utils.ui.StatusBarUtil; import java.io.File; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Action0; public class BreakpointContinuinglyActivity extends AppCompatActivity implements ProgressListener { private String PACKAGE_URL = "http://gdown.baidu.com/data/wisegame/df65a597122796a4/weixin_821.apk"; private ProgressBar progressBar; private Button button1, button2, button3; private long breakPoints; private ProgressDownloader downloader; private File file; private long totalBytes; private long contentLength; private boolean isLoading = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_breakpointcontinuingly); //根据状态栏颜色来决定 状态栏背景 用黑色还是白色 true:是否修改状态栏字体颜色 StatusBarUtil.setStatusBarMode(this, true, false, R.color.baise); progressBar = findViewById(R.id.progressBar); button1 = findViewById(R.id.downloadButton); button2 = findViewById(R.id.cancel_button); button3 = findViewById(R.id.continue_button); //下载 button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!isLoading) { breakPoints = 0L; file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "sample.apk"); downloader = new ProgressDownloader(PACKAGE_URL, file, BreakpointContinuinglyActivity.this); downloader.download(0L); isLoading = true; } } }); //暂停 button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isLoading) { downloader.pause(); Toast.makeText(BreakpointContinuinglyActivity.this, "下载暂停", Toast.LENGTH_SHORT).show(); // 存储此时的totalBytes,即断点位置。 breakPoints = totalBytes; isLoading = false; } } }); //继续 button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!isLoading) { downloader.download(breakPoints); isLoading = true; } } }); } /** * onPreExecute方法 * 文件总长只需记录一次,要注意断点续传后的contentLength只是剩余部分的长度 */ @Override public void onPreExecute(long contentLength) { if (this.contentLength == 0L) { this.contentLength = contentLength; progressBar.setMax((int) (contentLength / 1024)); } } /** * update方法 */ @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(BreakpointContinuinglyActivity.this, "下载完成", Toast.LENGTH_SHORT).show(); } }) .subscribe(); } } }
结果