简体   繁体   中英

Download multiple concurrent files

I need download multiple files in one time (About 100 files)

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.

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

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. It's easy to use and has many futures like connection pooling and reuse via HTTP Connection...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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