压缩下载优化
This commit is contained in:
parent
6b557374bf
commit
25b3076560
|
|
@ -54,7 +54,7 @@ public class DownloadController {
|
|||
@javax.annotation.Resource(name = "testTaskExecutor")
|
||||
private ThreadPoolTaskExecutor testTaskExecutor;
|
||||
|
||||
/*@PostMapping("/start")
|
||||
@PostMapping("/start")
|
||||
public ResponseEntity<String> 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<Resource> downloadFile(@RequestParam String taskId) throws IOException {
|
||||
public ResponseEntity<Resource> downloadFile(@RequestParam String taskId) {
|
||||
try {
|
||||
File file = downloadService.getDownloadFile(taskId);
|
||||
if (file == null || !file.exists()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
org.springframework.core.io.Resource resource = new FileSystemResource(file);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@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<Photo> getPhotosForAlbum(String proId,String type) {
|
||||
List<Photo> list = Optional.ofNullable(synthesisQueryDao.findByAlbumId(proId,type)).orElseGet(ArrayList::new);
|
||||
|
|
|
|||
|
|
@ -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<String, List<DownloadProgressListener>> progressListeners = new ConcurrentHashMap<>();
|
||||
private final Map<String, String> 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<SynthesisQueryVo> list = Optional.ofNullable(synthesisQueryDao.generateWatermark(proId)).orElseGet(ArrayList::new);
|
||||
list.forEach(item->{
|
||||
String path = SystemUtils.getUploadPath() + item.getOriginalFilePath();
|
||||
List<Future> futureList = new ArrayList<>();
|
||||
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()) {
|
||||
String syPath = generateWatermarkData(item);
|
||||
item.setWatermarkFilePath(syPath);
|
||||
String syPath = generateWatermarkData(vo);
|
||||
vo.setWatermarkFilePath(syPath);
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
});
|
||||
if(CollectionUtils.isNotEmpty(list)){
|
||||
synthesisQueryDao.updateBatchSyData(list);
|
||||
futureList.add(future);
|
||||
}
|
||||
for (Future<SynthesisQueryVo> future : futureList) {
|
||||
SynthesisQueryVo vo = future.get();
|
||||
newList.add(vo);
|
||||
}
|
||||
if(CollectionUtils.isNotEmpty(newList)){
|
||||
synthesisQueryDao.updateBatchSyData(newList);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.toString(),e);
|
||||
|
|
|
|||
|
|
@ -176,14 +176,6 @@ public class ParallelZipService {
|
|||
}, 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) {
|
||||
try {
|
||||
if (Files.exists(tempDir)) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue