简体   繁体   English

下载多个并发文件

[英]Download multiple concurrent files

I need download multiple files in one time (About 100 files)我需要一次下载多个文件(大约 100 个文件)

It does not matter whether the download is synchronized下载是否同步无所谓

And the important thing is that all files be downloaded.重要的是下载所有文件。

My code for getting urls and file names:我获取网址和文件名的代码:

for (int i = 0; i < AssetData.size(); i++){
        String item = AssetData.get(i).toString();
        String name[] = item.split("/");
        String Url = setting.Main_Domain+"/"+item;// Url for downloading
        String fname =name[name.length-1] ;// File name like: test.txt
        File file2 =  new File(getFilesDir(),item.replace(fname,"")); // Parent File like: data/user/0/com.test.app/data/
        if(!file2.exists()){file2.mkdir();}

    }

The size of the files is small and all together is about 3 megabytes.文件的大小很小,总共大约 3 兆字节。

You can implement your code with using this library.您可以使用此库来实现您的代码。 You can download multiple files concurrent or you can start next download after one is completed.您可以同时下载多个文件,也可以在一个文件完成后开始下一次下载。

https://github.com/MindorksOpenSource/PRDownloader https://github.com/MindorksOpenSource/PRDownloader

This is how your code will look这是您的代码的外观

int downloadId = PRDownloader.download(url, dirPath, fileName)
                        .build()
                        .setOnStartOrResumeListener(new OnStartOrResumeListener() {
                            @Override
                            public void onStartOrResume() {

                            }
                        })
                        .setOnPauseListener(new OnPauseListener() {
                            @Override
                            public void onPause() {

                            }
                        })
                        .setOnCancelListener(new OnCancelListener() {
                            @Override
                            public void onCancel() {

                            }
                        })
                        .setOnProgressListener(new OnProgressListener() {
                            @Override
                            public void onProgress(Progress progress) {

                            }
                        })
                        .start(new OnDownloadListener() {
                            @Override
                            public void onDownloadComplete() {

                            }

                            @Override
                            public void onError(Error error) {

                            }
                        });

Isn't this simple.这是不是很简单。 :) :)

This seems to me ion is a pretty elegant solution for you.在我看来ion对您来说是一个非常优雅的解决方案。 It's easy to use and has many futures like connection pooling and reuse via HTTP Connection...它易于使用并且具有许多未来,例如连接池和通过 HTTP 连接重用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM