压缩下载优化

This commit is contained in:
cwchen 2025-04-09 16:26:28 +08:00
parent 6b557374bf
commit 25b3076560
3 changed files with 52 additions and 32 deletions

View File

@ -54,7 +54,7 @@ public class DownloadController {
@javax.annotation.Resource(name = "testTaskExecutor") @javax.annotation.Resource(name = "testTaskExecutor")
private ThreadPoolTaskExecutor testTaskExecutor; private ThreadPoolTaskExecutor testTaskExecutor;
/*@PostMapping("/start") @PostMapping("/start")
public ResponseEntity<String> startDownload(@RequestBody DownloadRequest request) { public ResponseEntity<String> startDownload(@RequestBody DownloadRequest request) {
downloadService.startDownloadTask(request.getTaskId(), request.getProId(),request.getType(),request.getProName()); downloadService.startDownloadTask(request.getTaskId(), request.getProId(),request.getType(),request.getProName());
return ResponseEntity.ok("Download task started"); return ResponseEntity.ok("Download task started");
@ -110,21 +110,26 @@ public class DownloadController {
} }
@GetMapping("/file") @GetMapping("/file")
public ResponseEntity<Resource> downloadFile(@RequestParam String taskId) throws IOException { public ResponseEntity<Resource> downloadFile(@RequestParam String taskId) {
try {
File file = downloadService.getDownloadFile(taskId); File file = downloadService.getDownloadFile(taskId);
if (file == null || !file.exists()) { if (file == null || !file.exists()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
org.springframework.core.io.Resource resource = new FileSystemResource(file); Resource resource = new FileSystemResource(file);
return ResponseEntity.ok() return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, .header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + file.getName() + "\"") "attachment; filename=\"" + file.getName() + "\"")
.contentLength(file.length()) .contentLength(file.length())
.contentType(MediaType.APPLICATION_OCTET_STREAM) .contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource); .body(resource);
}*/ } catch (Exception e) {
log.error(e.toString(),e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@Autowired /*@Autowired
private ParallelZipService zipService; private ParallelZipService zipService;
@Autowired @Autowired
@ -245,7 +250,7 @@ public class DownloadController {
log.error("文件下载失败: {}", taskId, e); log.error("文件下载失败: {}", taskId, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
} }
} }*/
private List<Photo> getPhotosForAlbum(String proId,String type) { private List<Photo> getPhotosForAlbum(String proId,String type) {
List<Photo> list = Optional.ofNullable(synthesisQueryDao.findByAlbumId(proId,type)).orElseGet(ArrayList::new); List<Photo> list = Optional.ofNullable(synthesisQueryDao.findByAlbumId(proId,type)).orElseGet(ArrayList::new);

View File

@ -9,10 +9,12 @@ import com.bonus.imgTool.utils.HighQualityWatermark;
import com.bonus.imgTool.utils.SystemUtils; import com.bonus.imgTool.utils.SystemUtils;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -26,8 +28,10 @@ import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@ -53,6 +57,9 @@ public class DownloadService {
@Resource(name = "SynthesisQueryDao") @Resource(name = "SynthesisQueryDao")
private SynthesisQueryDao synthesisQueryDao; private SynthesisQueryDao synthesisQueryDao;
@javax.annotation.Resource(name = "testTaskExecutor")
private ThreadPoolTaskExecutor testTaskExecutor;
private final Map<String, List<DownloadProgressListener>> progressListeners = new ConcurrentHashMap<>(); private final Map<String, List<DownloadProgressListener>> progressListeners = new ConcurrentHashMap<>();
private final Map<String, String> taskFileMap = new ConcurrentHashMap<>(); private final Map<String, String> taskFileMap = new ConcurrentHashMap<>();
@ -83,6 +90,9 @@ public class DownloadService {
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
int processed = 0; int processed = 0;
for (Photo photo : photos) { for (Photo photo : photos) {
if(StringUtils.isBlank(photo.getFilePath())){
continue;
}
String path = SystemUtils.getUploadPath() + File.separator + photo.getFilePath(); String path = SystemUtils.getUploadPath() + File.separator + photo.getFilePath();
Path photoPath = Paths.get(path); Path photoPath = Paths.get(path);
if (!Files.exists(photoPath)) { // 文件不存在 if (!Files.exists(photoPath)) { // 文件不存在
@ -161,15 +171,28 @@ public class DownloadService {
try { try {
// 查询图片未生成水印照片的数据 // 查询图片未生成水印照片的数据
List<SynthesisQueryVo> list = Optional.ofNullable(synthesisQueryDao.generateWatermark(proId)).orElseGet(ArrayList::new); List<SynthesisQueryVo> list = Optional.ofNullable(synthesisQueryDao.generateWatermark(proId)).orElseGet(ArrayList::new);
list.forEach(item->{ List<Future> futureList = new ArrayList<>();
String path = SystemUtils.getUploadPath() + item.getOriginalFilePath(); List<SynthesisQueryVo> newList = new ArrayList<>();
for (SynthesisQueryVo vo : list) {
Future<SynthesisQueryVo> future = testTaskExecutor.submit(new Callable<SynthesisQueryVo>() {
@Override
public SynthesisQueryVo call() throws Exception {
String path = SystemUtils.getUploadPath() + vo.getOriginalFilePath();
if (new File(path).exists()) { if (new File(path).exists()) {
String syPath = generateWatermarkData(item); String syPath = generateWatermarkData(vo);
item.setWatermarkFilePath(syPath); vo.setWatermarkFilePath(syPath);
}
return vo;
} }
}); });
if(CollectionUtils.isNotEmpty(list)){ futureList.add(future);
synthesisQueryDao.updateBatchSyData(list); }
for (Future<SynthesisQueryVo> future : futureList) {
SynthesisQueryVo vo = future.get();
newList.add(vo);
}
if(CollectionUtils.isNotEmpty(newList)){
synthesisQueryDao.updateBatchSyData(newList);
} }
} catch (Exception e) { } catch (Exception e) {
logger.error(e.toString(),e); logger.error(e.toString(),e);

View File

@ -176,14 +176,6 @@ public class ParallelZipService {
}, executorService); }, executorService);
} }
private List<List<Photo>> partitionList(List<Photo> list, int size) {
List<List<Photo>> 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) { private void cleanupTempFiles(Path tempDir) {
try { try {
if (Files.exists(tempDir)) { if (Files.exists(tempDir)) {