多线程同时下载网页中的所有图片,完成后返回

java

浏览数:250

2019-1-13


DownloadImages.java

/**
 * 处理内容中包含的图片信息
 * @param content
 */
private List<String> handleImages(String articleUrl, Element content) {
    List<String> newImgUrls = new Vector<>();
    ExecutorService threadPool = Executors.newFixedThreadPool(MAX_IMAGE_NUM);//newCachedThreadPool();
    Set<String> imgUrls = new HashSet<>();//图片去重
    content.select("img").forEach(elem -> imgUrls.add(elem.absUrl("src")));
    imgUrls.forEach(imgUrl -> threadPool.execute(()-> {
        if(newImgUrls.size() < MAX_IMAGE_NUM) {
            String newUrl = downloadImageAndUploadToCloud(imgUrl, articleUrl);
            if (newUrl != null && newImgUrls.size() < MAX_IMAGE_NUM)
                newImgUrls.add(newUrl);
        }
    }));
    //等待所有线程执行完毕并返回结果
    try {
        threadPool.shutdown();
        threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException e) {
        log.error("Download images interrupted", e);
    }
    threadPool.shutdownNow();
    return newImgUrls;
}