DownloadImages.java /** * 处理内容中包含的图片信息 * @param content */private List handleImages(String articleUrl, Element content) { List newImgUrls = new Vector(); ExecutorService threadPool = Executors.newFixedThreadPool(MAX_
/** * 处理内容中包含的图片信息 * @param content */ private ListhandleImages(String articleUrl, Element content) { List newImgUrls = new Vector<>(); ExecutorService threadPool = Executors.newFixedThreadPool(MAX_IMAGE_NUM);//newCachedThreadPool(); Set 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; }