| | |
| | | import java.io.*; |
| | | import java.net.URL; |
| | | import java.util.List; |
| | | import java.util.concurrent.CountDownLatch; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | import javax.imageio.ImageIO; |
| | |
| | | public class ImageDownloaderAndCompressor { |
| | | |
| | | |
| | | private static final int THREAD_POOL_SIZE = 10; |
| | | |
| | | public static void downloadAndSaveImages(List<String> urls, String outputFolder) { |
| | | ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE); |
| | | CountDownLatch latch = new CountDownLatch(urls.size()); |
| | | int count = 0; |
| | | |
| | | for (String imageUrl : urls) { |
| | | try { |
| | | // 下载图片 |
| | | URL url = new URL(imageUrl); |
| | | BufferedImage image = ImageIO.read(url); |
| | | // 保存图片 |
| | | File outputfile = new File(outputFolder, "image_" + (count++) + ".jpg"); |
| | | ImageIO.write(image, "jpg", outputfile); |
| | | System.out.println("Saved: " + outputfile.getAbsolutePath()); |
| | | } catch (IOException e) { |
| | | System.err.println("Failed to download image: " + imageUrl); |
| | | e.printStackTrace(); |
| | | } |
| | | final int index = count++; |
| | | executorService.submit(() -> { |
| | | try { |
| | | // 下载图片 |
| | | URL url = new URL(imageUrl); |
| | | BufferedImage image = ImageIO.read(url); |
| | | // 保存图片 |
| | | File outputfile = new File(outputFolder, "image_" + index + ".jpg"); |
| | | ImageIO.write(image, "jpg", outputfile); |
| | | System.out.println("Saved: " + outputfile.getAbsolutePath()); |
| | | } catch (IOException e) { |
| | | System.err.println("Failed to download image: " + imageUrl); |
| | | e.printStackTrace(); |
| | | } finally { |
| | | latch.countDown(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | try { |
| | | latch.await(); |
| | | } catch (InterruptedException e) { |
| | | Thread.currentThread().interrupt(); |
| | | System.err.println("Image downloading was interrupted."); |
| | | } finally { |
| | | executorService.shutdown(); |
| | | } |
| | | } |
| | | |
| | | public static void zipAndSendFolder(String folderName, HttpServletResponse response) { |
| | | File zipFile = new File(folderName + ".zip"); |
| | | try { |
| | | File zipFile = new File(folderName + ".zip"); |
| | | zipFolder(folderName, zipFile.getPath()); |
| | | |
| | | // 设置响应头 |
| | |
| | | } |
| | | } |
| | | |
| | | // 删除临时压缩文件 |
| | | zipFile.delete(); |
| | | |
| | | } catch (IOException e) { |
| | | System.err.println("Failed to compress or send folder: " + folderName); |
| | | e.printStackTrace(); |
| | | response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
| | | try { |
| | | response.getWriter().write("Failed to compress or send folder: " + folderName); |
| | | } catch (IOException ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | | } finally { |
| | | // 删除临时压缩文件 |
| | | if (zipFile.exists()) { |
| | | zipFile.delete(); |
| | | } |
| | | } |
| | | } |
| | | |