A good way to bulk download images over http with Java

我怕爱的太早我们不能终老 提交于 2019-11-30 21:09:51

The Executor framework is EXACTLY what you want. Specifically the ExecutorCompletionService. Using this, you'll be able to submit requests quickly and in any order. Then you'll retrieve them exactly as they completed (as opposed to submission order).

Here is my take using Resty. (DISCLAIMER: I'm the author of Resty) Downloads all URLs given on the command line and prints out the file names.

package us.monoid.web.parallel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import us.monoid.web.Resty;

public class Downloader {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        List<Callable<File>> tasks = new ArrayList<Callable<File>>(args.length);
        for (final String url : args) {
            tasks.add(new Callable<File>() {
                public File call() throws Exception {
                    return new Resty().bytes(url).save(File.createTempFile("img", ".png"));
                }               
            });
        }
        List<Future<File>> results = pool.invokeAll(tasks);
        for (Future<File> ff : results) {
            System.out.println(ff.get());
        }
    }

}

Using Resty Library images can be downloaded with custom names as follows

try {
        ExecutorService pool = Executors.newFixedThreadPool(Names.size());
         List<Callable<File>> tasks = new ArrayList<Callable<File>>(Names.size());

         for (final String url : Urls) {

             tasks.add(new Callable<File>() {
               public File call() throws Exception {

                     File f=new File(directory+iimage);

                     return new Resty().bytes(url).save(f);

                 }

             });
             i++;
         }
         i=0;
         List<Future<File>> results = pool.invokeAll(tasks);
         for (Future<File> ff : results) {
             System.out.println(ff.get());
         }
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        fails++;
        e.printStackTrace();

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!