今天上1篇博客恰好在郭神公众号出现了。也有1个多月没写点甚么了。今天就继上1次的okhttp继续深入了解把。在你必须学会的okhttp——入门篇中我简单介绍了okhttp的使用方法。不了解可以看完在回来看这篇文章。
好了。话不多说。这次我主要介绍下okhttp如何实现多文件断点下载。
参考博客:
http://blog.csdn.net/KevinsCSDN/article/details/51934274
之前对如何使用okhttp上传与下载我们已知道该怎样做了。但是如何实现多文件的操作呢?首先,在这边论述下我做的进程中所遇到的问题。
我在网上看到很多demo对这块是用SQLite实现,我觉得完全可以Shareperference来存储,通过他的url来存储对应确当前长度和总长度,有人回说Shareperference不是只能1个建对应1个值么,两个怎样解决。我们可以通过MD5加密的url来存储当前进度。通过MD2加密来存储总进度。
我们可以通过okhttp自带的拦截器来实现其效果,具体代码以下:
private Call newCall(long current_length ) {
Request request = new Request.Builder()
.url(url)
.header("RANGE", "bytes=" + current_length + "-")
.build();
return client.newCall(request);
}
public OkHttpClient getProgressClient() {
Interceptor interceptor = new Interceptor() {
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.body(originalResponse.body())
.build();
}
};
return new OkHttpClient.Builder()
.addNetworkInterceptor(interceptor)
.build();
}
其实在前面我也说了。用Shareperference来实现,具体怎样说的。你的url,存储路径每次都是需要传的,但是为了避免进度冲突(例:明明的A的进度,下载B的时候却用的A的进度)。所以通过传入的url来用Shareperference得到他存储确当前长度与总长度来解决。
其实。。。我也不知道。。百度了好久。得到的答案是在HTTP协议中,消息实体长度和消息实体的传输长度是有区分,比如说gzip紧缩下,消息实体长度是紧缩前的长度,消息实体的传输长度是gzip紧缩后的长度。还有种说法还有种说法是服务器限制问题。不解。总之会致使获得进度的时候,进度值是大于100的。。。
大致的问题和解决方法已说明了。首先,我们先来看下效果图。
最后上源码,相信你看懂了上面的思路。对源码的理解就不是很难了。
public class DownLoadSupport {
private OkHttpClient okHttpClient;
private Call call;
@Bean
FileSupport fileSupport;
@Bean
ByteUtils byteUtils;
@Bean
SharePreferencesUtils sharePreferencesUtils;
private MD5Utils md5Utils;
public DownLoadSupport() {
md5Utils = new MD5Utils();
okHttpClient = getProgressClient();
}
public OkHttpClient getProgressClient() {
Interceptor interceptor = new Interceptor() {
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.body(originalResponse.body())
.build();
}
};
return new OkHttpClient.Builder().addNetworkInterceptor(interceptor).build();
}
private Call newCall(HttpDownloadBean httpDownloadBean) {
Request request = new Request.Builder().tag(md5Utils.md5(httpDownloadBean.getUrl()))
.url(httpDownloadBean.getUrl()).addHeader("Accept-Encoding", "identity")
.header("RANGE", "bytes=" + sharePreferencesUtils.get(md5Utils.md5(httpDownloadBean.getUrl()), (long) 0) + "-")
.build();
return okHttpClient.newCall(request);
}
public void download(final HttpDownloadBean httpDownloadBean, final DownloadCallBack callBack) {
if (!sharePreferencesUtils.contains(md5Utils.md5(httpDownloadBean.getUrl()))) {
sharePreferencesUtils.put(md5Utils.md5(httpDownloadBean.getUrl()), (long) 0);
}
if (!sharePreferencesUtils.contains(md5Utils.md2(httpDownloadBean.getUrl()))) {
sharePreferencesUtils.put(md5Utils.md2(httpDownloadBean.getUrl()), (long) 0);
}
call = newCall(httpDownloadBean);
call.enqueue(new Callback() {
public void onFailure(Call call, IOException e) {
}
public void onResponse(Call call, Response response) throws IOException {
writeToSDCard(response, httpDownloadBean, callBack);
}
});
}
public void pause(HttpDownloadBean httpDownloadBean) {
for (Call call : okHttpClient.dispatcher().queuedCalls()) {
if (call.request().tag().equals(md5Utils.md5(httpDownloadBean.getUrl())))
call.cancel();
}
for (Call call : okHttpClient.dispatcher().runningCalls()) {
if (call.request().tag().equals(md5Utils.md5(httpDownloadBean.getUrl())))
call.cancel();
}
}
private void writeToSDCard(Response response, HttpDownloadBean httpDownloadBean, DownloadCallBack callBack) {
ResponseBody body = response.body();
InputStream input = body.byteStream();
FileChannel channelOut = null;
// 随机访问文件,可以指定断点续传的起始位置
RandomAccessFile randomAccessFile = null;
long current = 0;
long total = 0;
current = (long) sharePreferencesUtils.get(md5Utils.md5(httpDownloadBean.getUrl()), (long) 0);
total = (long) sharePreferencesUtils.get(md5Utils.md2(httpDownloadBean.getUrl()), (long) 0);
if (total == 0) {
total = body.contentLength();
httpDownloadBean.setTotal_length(body.contentLength());
sharePreferencesUtils.put(md5Utils.md2(httpDownloadBean.getUrl()), httpDownloadBean.getTotal_length());
}
try {
randomAccessFile = new RandomAccessFile(fileSupport.createStorgeFile(httpDownloadBean.getStoragepath(), httpDownloadBean.getFilepath()), "rwd");
//Chanel NIO中的用法,由于RandomAccessFile没有使用缓存策略,直接使用会使得下载速度变慢,亲测缓存下载3.3秒的文件,用普通的RandomAccessFile需要20多秒。
channelOut = randomAccessFile.getChannel();
// 内存映照,直接使用RandomAccessFile,是用其seek方法指定下载的起始位置,使用缓存下载,在这里指定下载位置。
MappedByteBuffer mappedBuffer = channelOut.map(FileChannel.MapMode.READ_WRITE, current, total);
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) != -1) {
current += len;
if (callBack != null) {
callBack.download(byteUtils.getSize(current) + byteUtils.getByte(current), byteUtils.getSize(total) + byteUtils.getByte(total));
callBack.downloadprogress((int) (current * 1.0f / total * 100));
}
httpDownloadBean.setCurrent_length(current);
if (current >= total) {
sharePreferencesUtils.remove(md5Utils.md5(httpDownloadBean.getUrl()));
sharePreferencesUtils.remove(md5Utils.md2(httpDownloadBean.getUrl()));
} else {
sharePreferencesUtils.put(md5Utils.md5(httpDownloadBean.getUrl()), httpDownloadBean.getCurrent_length());
}
mappedBuffer.put(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
if (channelOut != null) {
channelOut.close();
}
if (randomAccessFile != null) {
randomAccessFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public interface DownloadCallBack {
void download(String current_progress, String total_progress);
void downloadprogress(int progress);
}
}
关于httpdownloadbean:
`
public class HttpDownloadBean {
private String url = null;
private String storagepath = null;
private String filepath = null;
private long current_length = 0L;
private long total_length = 0L;
public long getTotal_length() {
return total_length;
}
public void setTotal_length(long total_length) {
this.total_length = total_length;
}
public long getCurrent_length() {
return current_length;
}
public void setCurrent_length(long current_length) {
this.current_length = current_length;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getStoragepath() {
return storagepath;
}
public void setStoragepath(String storagepath) {
this.storagepath = storagepath;
}
public String getFilepath() {
return filepath;
}
public void setFilepath(String path) {
this.filepath = path;
}
}
“`
就是这几个数据,通过set和get来设置和获得。
ShareperferenceUtils是关于Shareperference的工具类。filesupport是用来创建文件的。byteutils可以没必要理睬。
有甚么问题可以提出来1起讨论。这应当是年前最后1篇技术博文了。我也该为年后找工作的事件而繁忙了。