使用了一个第三方库:filedownloader
引用
compile 'com.liulishuo.filedownloader:library:1.6.9'
使用:
//下载单个文件
public static void downloadSingleFile(String url, String savePath, Context context, final DownloadCallBack callBack){
FileDownloader.setup(context);
if(downloader == null){
downloader = FileDownloader.getImpl();
}
downloader.pauseAll();
downloader.setMaxNetworkThreadCount(15);
downloader.create(url)
.setPath(savePath).setAutoRetryTimes(2)
.setListener(new FileDownloadLargeFileListener() {
@Override
protected void pending(BaseDownloadTask task, long soFarBytes, long totalBytes) {
}
@Override
protected void progress(BaseDownloadTask task, long soFarBytes, long totalBytes) {
callBack.onProgress((int)((float)soFarBytes/totalBytes*100),task.getSpeed());
}
@Override
protected void paused(BaseDownloadTask task, long soFarBytes, long totalBytes) {
}
@Override
protected void completed(BaseDownloadTask task) {
callBack.onCompleted(task.getTargetFilePath());
}
@Override
protected void error(BaseDownloadTask task, Throwable e) {
callBack.onFail(e.toString());
}
@Override
protected void warn(BaseDownloadTask task) {
}
}).start();
}
private static int totalNum = 0;
public static void downloadMuiltyFiles(final List<String> urls, List<String> savePaths, Context context, final DownloadCallBack callBack){ if(urls == null || savePaths == null || urls.size() == 0 || urls.size() != savePaths.size()){ callBack.onCompleted(""); return; } final List<String> actUrls = new ArrayList<>(); List<String> actPaths = new ArrayList<>(); File file = null; int j = 0; for(String p:savePaths){ file = new File(p); if(!file.exists()){ actUrls.add(urls.get(j)); actPaths.add(p); } j++; } if(actPaths.size() == 0){ callBack.onCompleted(""); return; } FileDownloader.setup(context); totalNum = actUrls.size(); final FileDownloadListener downloadListener = new FileDownloadListener() { @Override protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) { } @Override protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) { } @Override protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) { } @Override protected void blockComplete(BaseDownloadTask task) { } @Override protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) { } @Override protected void completed(BaseDownloadTask task) { totalNum--;//下载多个文件
//多文件下载时,每下载一个文件,回调一次completed if(totalNum <= 0){ callBack.onCompleted(task.getTargetFilePath()); }else{ int downNum = actUrls.size()-totalNum; callBack.onNumProgress((int)((float)downNum/actUrls.size()*100),downNum,actUrls.size(),task.getSpeed()); } } @Override protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) { } @Override protected void error(BaseDownloadTask task, Throwable e) { callBack.onFail(e.toString()); } @Override protected void warn(BaseDownloadTask task) { } }; final FileDownloadQueueSet queueSet = new FileDownloadQueueSet(downloadListener); final List<BaseDownloadTask> tasks = new ArrayList<>(); for (int i = 0; i < urls.size(); i++) { tasks.add(FileDownloader.getImpl().create(urls.get(i)).setPath(actPaths.get(i)).setTag(i + 1)); } // 由于是队列任务, 这里是我们假设了现在不需要每个任务都回调`FileDownloadListener#progress`, 我们只关系每个任务是否完成, 所以这里这样设置可以很有效的减少ipc. queueSet.disableCallbackProgressTimes(); // 所有任务在下载失败的时候都自动重试一次 queueSet.setAutoRetryTimes(1); queueSet.downloadTogether(tasks); queueSet.start(); } public interface DownloadCallBack{ void onCompleted(String filename); void onProgress(int progress, int speed); void onNumProgress(int progress, int downNum, int totalNum, int speed); void onFail(String error); }