From 25b307656087e0e6a34845ee30cdf038082ed56a Mon Sep 17 00:00:00 2001 From: cwchen <1048842385@qq.com> Date: Wed, 9 Apr 2025 16:26:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E4=B8=8B=E8=BD=BD=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DownloadController.java | 35 +++++++++------- .../backstage/service/DownloadService.java | 41 +++++++++++++++---- .../backstage/service/ParallelZipService.java | 8 ---- 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/bonus/imgTool/backstage/controller/DownloadController.java b/src/main/java/com/bonus/imgTool/backstage/controller/DownloadController.java index 052cdc5..30d2239 100644 --- a/src/main/java/com/bonus/imgTool/backstage/controller/DownloadController.java +++ b/src/main/java/com/bonus/imgTool/backstage/controller/DownloadController.java @@ -54,7 +54,7 @@ public class DownloadController { @javax.annotation.Resource(name = "testTaskExecutor") private ThreadPoolTaskExecutor testTaskExecutor; - /*@PostMapping("/start") + @PostMapping("/start") public ResponseEntity startDownload(@RequestBody DownloadRequest request) { downloadService.startDownloadTask(request.getTaskId(), request.getProId(),request.getType(),request.getProName()); return ResponseEntity.ok("Download task started"); @@ -110,21 +110,26 @@ public class DownloadController { } @GetMapping("/file") - public ResponseEntity downloadFile(@RequestParam String taskId) throws IOException { - File file = downloadService.getDownloadFile(taskId); - if (file == null || !file.exists()) { - return ResponseEntity.notFound().build(); + public ResponseEntity downloadFile(@RequestParam String taskId) { + try { + File file = downloadService.getDownloadFile(taskId); + if (file == null || !file.exists()) { + return ResponseEntity.notFound().build(); + } + Resource resource = new FileSystemResource(file); + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_DISPOSITION, + "attachment; filename=\"" + file.getName() + "\"") + .contentLength(file.length()) + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .body(resource); + } catch (Exception e) { + log.error(e.toString(),e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } - org.springframework.core.io.Resource resource = new FileSystemResource(file); - return ResponseEntity.ok() - .header(HttpHeaders.CONTENT_DISPOSITION, - "attachment; filename=\"" + file.getName() + "\"") - .contentLength(file.length()) - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .body(resource); - }*/ + } - @Autowired + /*@Autowired private ParallelZipService zipService; @Autowired @@ -245,7 +250,7 @@ public class DownloadController { log.error("文件下载失败: {}", taskId, e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } - } + }*/ private List getPhotosForAlbum(String proId,String type) { List list = Optional.ofNullable(synthesisQueryDao.findByAlbumId(proId,type)).orElseGet(ArrayList::new); diff --git a/src/main/java/com/bonus/imgTool/backstage/service/DownloadService.java b/src/main/java/com/bonus/imgTool/backstage/service/DownloadService.java index d37edac..505c618 100644 --- a/src/main/java/com/bonus/imgTool/backstage/service/DownloadService.java +++ b/src/main/java/com/bonus/imgTool/backstage/service/DownloadService.java @@ -9,10 +9,12 @@ import com.bonus.imgTool.utils.HighQualityWatermark; import com.bonus.imgTool.utils.SystemUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -26,8 +28,10 @@ import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; import java.util.*; +import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.Future; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -53,6 +57,9 @@ public class DownloadService { @Resource(name = "SynthesisQueryDao") private SynthesisQueryDao synthesisQueryDao; + @javax.annotation.Resource(name = "testTaskExecutor") + private ThreadPoolTaskExecutor testTaskExecutor; + private final Map> progressListeners = new ConcurrentHashMap<>(); private final Map taskFileMap = new ConcurrentHashMap<>(); @@ -83,6 +90,9 @@ public class DownloadService { byte[] buffer = new byte[8192]; int processed = 0; for (Photo photo : photos) { + if(StringUtils.isBlank(photo.getFilePath())){ + continue; + } String path = SystemUtils.getUploadPath() + File.separator + photo.getFilePath(); Path photoPath = Paths.get(path); if (!Files.exists(photoPath)) { // 文件不存在 @@ -161,15 +171,28 @@ public class DownloadService { try { // 查询图片未生成水印照片的数据 List list = Optional.ofNullable(synthesisQueryDao.generateWatermark(proId)).orElseGet(ArrayList::new); - list.forEach(item->{ - String path = SystemUtils.getUploadPath() + item.getOriginalFilePath(); - if (new File(path).exists()) { - String syPath = generateWatermarkData(item); - item.setWatermarkFilePath(syPath); - } - }); - if(CollectionUtils.isNotEmpty(list)){ - synthesisQueryDao.updateBatchSyData(list); + List futureList = new ArrayList<>(); + List newList = new ArrayList<>(); + for (SynthesisQueryVo vo : list) { + Future future = testTaskExecutor.submit(new Callable() { + @Override + public SynthesisQueryVo call() throws Exception { + String path = SystemUtils.getUploadPath() + vo.getOriginalFilePath(); + if (new File(path).exists()) { + String syPath = generateWatermarkData(vo); + vo.setWatermarkFilePath(syPath); + } + return vo; + } + }); + futureList.add(future); + } + for (Future future : futureList) { + SynthesisQueryVo vo = future.get(); + newList.add(vo); + } + if(CollectionUtils.isNotEmpty(newList)){ + synthesisQueryDao.updateBatchSyData(newList); } } catch (Exception e) { logger.error(e.toString(),e); diff --git a/src/main/java/com/bonus/imgTool/backstage/service/ParallelZipService.java b/src/main/java/com/bonus/imgTool/backstage/service/ParallelZipService.java index a41ca0d..b05bc56 100644 --- a/src/main/java/com/bonus/imgTool/backstage/service/ParallelZipService.java +++ b/src/main/java/com/bonus/imgTool/backstage/service/ParallelZipService.java @@ -176,14 +176,6 @@ public class ParallelZipService { }, executorService); } - private List> partitionList(List list, int size) { - List> partitions = new ArrayList<>(); - for (int i = 0; i < list.size(); i += size) { - partitions.add(list.subList(i, Math.min(i + size, list.size()))); - } - return partitions; - } - private void cleanupTempFiles(Path tempDir) { try { if (Files.exists(tempDir)) {