基于XUtil工具的多个文件下载显示总进度

项目开发中遇到这么一个问题,ListView的一个Item中显示多个文件,需要网络下载时,显示该Item中所有文件的一个总的下载进度,

比如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: 下载失败
            }
        });
    }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值