Download multiple concurrent files

180
April 15, 2018, at 9:45 PM

I need download multiple files (About 100 files)

It does not matter whether the download is synchronized

And just it is important that all files be downloaded.

My code java for get name files and urls:

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

Answer 1

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. :)

Answer 2

Keep track and follow the best practises, I recommand to learn and use RxJava and Retrofit2 to download your files, rxjava will take care about asynchronous tasks and retrofit will simplify downloading the concrete files, check this answer Download and write a file with Retrofit and RxJava

Rent Charter Buses Company
READ ALSO
Best language to learn JAVA or Node.js [on hold]

Best language to learn JAVA or Node.js [on hold]

Im looking at a couple of coding bootcampsNode

157
Type conversion for a String in an array throws &ldquo;Unknown Source Error&rdquo; in Java

Type conversion for a String in an array throws “Unknown Source Error” in Java

I am trying to parse a CSV file with java and get some ids

127
How would i know if Certain class&#39;s object creation would be expensive

How would i know if Certain class's object creation would be expensive

I assume If a thread unsafe class's object creation is expensive and we would like to use that instance at different place for thread execution in multi threaded environment, we should use ThreadLocalBecause it is expensive and it is unnecessary to create...

168