基于XUtil工具的多个文件下载显示总进度
2016-11-13 13:54
477 查看
项目开发中遇到这么一个问题,ListView的一个Item中显示多个文件,需要网络下载时,显示该Item中所有文件的一个总的下载进度,
比如item中有,file1、file2、file3、file4,其中file2为.mp4文件,其余为.jpg文件,下载之前不知道各个文件的大小,只有当XUtil的onLoading时,才能知道这个文件的总大小。
本文采用一中比较传统的方式实现,首先是一个文件类 FileModel 两个属性,fileName与filePath,另一个是下载进度类DownloadProgress,属性为下载量,缓存值,当前值,上一个文件下载完成时的值,总进度值,以及下载进度(百分比)。具体类参看代码:
下载时改选中的item中的List<FileModel> list,多个选中,则多次调用改函数接口。
比如item中有,file1、file2、file3、file4,其中file2为.mp4文件,其余为.jpg文件,下载之前不知道各个文件的大小,只有当XUtil的onLoading时,才能知道这个文件的总大小。
本文采用一中比较传统的方式实现,首先是一个文件类 FileModel 两个属性,fileName与filePath,另一个是下载进度类DownloadProgress,属性为下载量,缓存值,当前值,上一个文件下载完成时的值,总进度值,以及下载进度(百分比)。具体类参看代码:
public class FileModel { private String fileName = ""; private String filePath = ""; public FileModel() { } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } }
public class DownloadProgress { public long current = 0; public long total = 0; public long temp = 0; public long last = 0; public int result = 0; public DownloadProgress() { this.current = 0; this.result = 0; this.total = 0; this.last = 0; this.temp = 0; } public long getCurrent() { return current; } public void setCurrent(long current) { this.current = current; } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public long getTemp() { return temp; } public void setTemp(long temp) { this.temp = temp; } public long getLast() { return last; } public void setLast(long last) { this.last = last; } public int getResult() { return result; } public void setResult(int result) { this.result = result; } }
下载时改选中的item中的List<FileModel> list,多个选中,则多次调用改函数接口。
public void download(List<FileModel> fileList) { DownloadProgress progress = new DownloadProgress(); //每个需要下载的Item实例化不同的进度监看对象 int size = fileList.size(); if (size > 0) { for (int i = 0; i < size; i++) { String localPath = LOCAL_PATH + fileList.get(i).getFileName(); //下载文件的本地存放路径 downloadFile(fileList.get(i).getFilePath(), localPath, progress); } } } public void downloadFile(final String filePath, String localPath, final DownloadProgress progress) { HttpUtils http = new HttpUtils(); http.configRequestThreadPoolSize(5); //同时下载文件为5个 http.download(filePath, localPath, true, true, new RequestCallBack<File>() { @Override public void onStart() { Log.d(TAG, "start download file: " + filePath); } @Override public void onLoading(long total, long current, boolean isUploading) { // 显示进度条 百分之几 int result = (int) ((double) current / (double) total * 100); Log.d(TAG, "single file result=" + current + "/" + total + " = " + result + " % "); //文件一开始下载,设置这个文件的总值,并累计到progress中 if ((progress.temp != total) && (current == 0)) { progress.temp = total; progress.total += total; } //某个文件下载文成时,记录该文件总下载量 if (result == 100) { progress.last += total; progress.current = progress.last; } else { progress.current = progress.last + current; //未下载完成时,当前进度值为上一次的值加上当前值 } double tempResult = (double) (progress.current) / (double) (progress.total); //总进度 progress.result = (int) (tempResult * 100); Log.d(TAG, "model result=: " + progress.current + "/" + progress.total + " = " + progress.result + "%"); } @Override public void onSuccess(ResponseInfo<File> responseInfo) { // TODO:下载完成 } @Override public void onFailure(HttpException error, String msg) { // TODO: 下载失败 } }); }
相关文章推荐
- 基于Retrofit+Rxjava实现带进度显示的下载文件
- 用BCB的INDY组件中的IDHTTP 下载文件并显示出进度条
- VC下载文件 + 显示进度条
- Windows Phone 7 文件下载进度和速度显示
- Windows Phone 7 文件下载进度和速度显示
- C#下载文件(可显示进度)的方法实现
- 基于Java Web的文件上传进度显示的一种解决方案(转)
- C# Winform下载文件并显示进度条
- NSURLConnection下载文件时,如何显示进度
- Windows Phone 7 文件下载进度和速度显示
- (二)使用libcurl实现获取目标文件大小, 下载进度显示, 断点续传等功能
- 利用curl下载文件(进度条显示) 代码片段
- 基于Java Web的文件上传进度显示的一种解决方案
- C# Winform下载文件并显示进度条
- 基于Java Web的文件上传进度显示的一种解决方案
- 利用curl下载文件(进度条显示) 代码片段
- C++Builder利用URLDownloadToFile下载文件并显示进度
- Symbian中Http下载文件引擎,支持进度显示
- Android 下载文件 显示进度条
- 基于Java Web的文件上传进度显示的一种解决方案