java 下载支持断点续传_GitHub - 1621740748/FastDownload: java多线程队列下载,支持断点续传,大文件切割下载...

FastDownload

功能

多线程队列下载

断点续传

大文件切割块下载

设计理念

专注下载核心功能,提供UI状态回调,功能全面,使用简单

依赖

okhttp3(com.squareup.okhttp3), json(org.json.json)

初始化

实现持久化接口,以便能缓存下载进度,下面以Android为例

CacheInterface cache = new CacheInterface() {

@Override

public String read() {

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

return preferences.getString("downloadBreakPoint","");

}

@Override

public void write(String data) {

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

SharedPreferences.Editor editor = preferences.edit();

editor.putString("downloadBreakPoint", data);

editor.commit();

}

};

DownloadBreakPoint.setCacheInterface(cache);

调用

设置需要下载的资源列表

ArrayList resources = new ArrayList<>();

String sourceURL = "https://csdnimg.cn/public/common/libs/jquery/jquery-1.9.1.min.js";

String savePath = IOUtils.getAppDir() + "/jq.js";

String md5 = "ODdx7xaSv8w/K2kXyphXeA==";

long size = 92633L;

/*

@sourceURL 资源下载地址

@savePath 资源本地保存路径

@md5 资源哈希md5值

@size 资源大小

*/

Resource resource = new Resource(sourceURL, savePath, md5, size, null);

resources.add(resource);

监听下载状态

DownloadProgress downloadProgress = new DownloadProgress() {

@Override

protected synchronized void onDownloadChange() {

//Message.sent(SynchronousHandler.CHANGE_LOADING, new long[]{this.getLoaded(), this.getTotal()});

}

@Override

protected synchronized void onDownloadFailed(Resource resource) {

//Log.i("c3","failed: "+resource.getPath());

}

@Override

protected synchronized void onDownloadSuccess(Resource resource) {

//Log.i("c3","success: "+resource.getPath());

}

@Override

protected void onDownloadFinish() {

//下载队列执行完成 包括成功和失败的任务

//Log.i("c3","download all finish.");

}

};

构建下载任务

int nThreads = 5;

Long chunkSize = 5*1024*1024L;

/*

@nThreads 多线程数量上限

@chunkSize 资源切割块大小

*/

BatchDownload batchDownload = BatchDownload.build(resources, downloadProgress, nThreads, chunkSize);

OK, 这样就完成了调用

如果需要动态执行下载任务可以

batchDownload.download(resource);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值