简体   繁体   English

如何使用递归操作从不同的 URL 下载多个文件?

[英]How to download multiple files from different URLs using recursiveaction?

I used ExecutorService for downloading files from different URLs concurrently but the time it takes is not small compared to sequential downloading;我使用 ExecutorService 同时从不同的 URL 下载文件,但与顺序下载相比,它花费的时间并不短; therefore, I want to use RecursiveAction.因此,我想使用 RecursiveAction。 Below is the code to be applied:下面是要应用的代码:

Parallel class:平行类:

String[] links;
File[] files;

public Parallel(String[] link, File[] files) {
    this.links = link;
    this.files = files;
}

@Override
public void run() {
    try {
        for (int i = 0; i <= 3; i++) {
            URL url = new URL(links[i]);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();
            double fileSize = (double) http.getContentLengthLong();
            BufferedInputStream bis = new BufferedInputStream(http.getInputStream());
            FileOutputStream fos = new FileOutputStream(this.files[i]);
            BufferedOutputStream bos = new BufferedOutputStream(fos, 600000);
            byte[] buffer = new byte[1024];
            double downloadedData = 0.00;
            int readData = 0;

            while ((readData = bis.read(buffer, 0, 1024)) >= 0) {
                bos.write(buffer, 0, readData);
                downloadedData += readData;
            }
            bos.close();
            bis.close();
            System.out.println(this.files[i] + " -> done");
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

In main class, I just give the links and paths to files and start the execution在主类中,我只提供文件的链接和路径并开始执行

ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        Runnable worker = new Parallel(links, files);
        executor.execute(worker);
        executor.shutdown();

Any help is appreciated!任何帮助表示赞赏!

You are not using concurrency properly here.您在这里没有正确使用并发。

What you should do is something like that:你应该做的是这样的:

String link;
File file;

public Parallel(String link, File file) {
    this.link = link;
    this.file = files;
}

@Override
public void run() {
    try {
        URL url = new URL(link);
        HttpURLConnection http = (HttpURLConnection) url.openConnection();
        double fileSize = (double) http.getContentLengthLong();
        BufferedInputStream bis = new BufferedInputStream(http.getInputStream());
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos, 600000);
        byte[] buffer = new byte[1024];
        double downloadedData = 0.00;
        int readData = 0;

        while ((readData = bis.read(buffer, 0, 1024)) >= 0) {
            bos.write(buffer, 0, readData);
            downloadedData += readData;
        }
        bos.close();
        bis.close();
        System.out.println(file + " -> done");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

And then:进而:


String[] links;
File[] files;

//...

ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

for (int i = 0; i <= 3; i++) {
    Runnable worker = new Parallel(links[i], files[i]);
    executor.execute(worker);
}
executor.shutdown();

Then each download would actually get its own thread.然后每次下载实际上都会有自己的线程。

In your case all downloads get one thread where it all happens sequentally.在您的情况下,所有下载都会得到一个线程,所有这些都依次发生。

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

相关问题 使用servlet,如何从数据库下载多个文件并将其压缩以供客户端下载 - Using a servlet, how do you download multiple files from a database and zip them for client download 如何从单个超链接下载多个文件 - How to download multiple files from a single hyperlink RecursiveAction 如何与斐波那契一起工作? - How does RecursiveAction work with Fibonacci? 我们如何使用 Java SDK 从 S3 存储桶下载没有文件夹的多个文件 - How can we download multiple files without folder from S3 bucket using Java SDK 如何使用Java在Rest Web Service中下载不同类型的文件 - how to download different type of files in rest web service using java 无法从RecursiveAction类调用coInvoke函数 - Cannot call coInvoke function from RecursiveAction class 如何在Android中使用intentservice同时下载多个文件? - How to download multiple files concurrently using intentservice in Android? 如何使用 Liferay 将多个文件下载为 zip 文件? - How to download multiple files as a zip-file using Liferay? 从网址下载5张图片 - Download 5 images from urls 如何一一下载所有URL并保存在不同的文件夹中 - How to download All the URLs one by one and keep in different folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM