aix
2024-08-06 7f1e5a861d9944cc28285cd36ee47b33dee04446
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.dji.sample.media.util;
 
import java.awt.image.BufferedImage;
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;
import javax.servlet.http.HttpServletResponse;
 
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) {
            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 {
            zipFolder(folderName, zipFile.getPath());
 
            // 设置响应头
            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
            response.setContentLength((int) zipFile.length());
 
            // 将压缩文件写入响应
            try (InputStream is = new FileInputStream(zipFile);
                 OutputStream os = response.getOutputStream()) {
                byte[] buffer = new byte[1024];
                int length;
                while ((length = is.read(buffer)) > 0) {
                    os.write(buffer, 0, length);
                }
            }
 
        } 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();
            }
        }
    }
 
    public static void zipFolder(String srcFolder, String destZipFile) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(destZipFile);
             ZipOutputStream zos = new ZipOutputStream(fos)) {
 
            File srcFile = new File(srcFolder);
            for (File file : srcFile.listFiles()) {
                try (FileInputStream fis = new FileInputStream(file)) {
                    ZipEntry zipEntry = new ZipEntry(file.getName());
                    zos.putNextEntry(zipEntry);
 
                    byte[] bytes = new byte[1024];
                    int length;
                    while ((length = fis.read(bytes)) >= 0) {
                        zos.write(bytes, 0, length);
                    }
                    zos.closeEntry();
                }
            }
        }
    }
 
    public static void cleanUp(String folderName) {
        File folder = new File(folderName);
        deleteFolder(folder);
    }
 
    public static void deleteFolder(File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFolder(f);
                } else {
                    f.delete();
                }
            }
        }
        folder.delete();
    }
 
}