From bf7a9ef439e3b49268974d564107999619b2b4fd Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Sat, 20 Jul 2024 09:28:40 +0800
Subject: [PATCH] 优化图片压缩下载使用多线程
---
src/main/java/com/dji/sample/media/util/ImageDownloaderAndCompressor.java | 62 +++++++++++++++++++++++--------
1 files changed, 46 insertions(+), 16 deletions(-)
diff --git a/src/main/java/com/dji/sample/media/util/ImageDownloaderAndCompressor.java b/src/main/java/com/dji/sample/media/util/ImageDownloaderAndCompressor.java
index d5f98b8..3818a95 100644
--- a/src/main/java/com/dji/sample/media/util/ImageDownloaderAndCompressor.java
+++ b/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();
+ }
}
}
--
Gitblit v1.9.3