rain
2024-07-20 bf7a9ef439e3b49268974d564107999619b2b4fd
src/main/java/com/dji/sample/media/util/ImageDownloaderAndCompressor.java
@@ -4,6 +4,9 @@
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;
@@ -12,27 +15,46 @@
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());
            // 设置响应头
@@ -50,12 +72,20 @@
                }
            }
            // 删除临时压缩文件
            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();
            }
        }
    }