From efb1963edecd0ca0c53bdd037c85b4e534c5eb8a Mon Sep 17 00:00:00 2001 From: wangguangyuan Date: Wed, 9 Jul 2025 15:17:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=AF=A6=E6=83=85=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E4=BC=98=E5=8C=96=EF=BC=9A=E5=8E=8B=E7=BC=A9=E5=9B=BE?= =?UTF-8?q?=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inspect-main/inspect-main-task/pom.xml | 14 ++++ .../task/controller/PatrolTaskController.java | 67 ++++++++++++++----- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/inspect-main/inspect-main-task/pom.xml b/inspect-main/inspect-main-task/pom.xml index 05bfa30..cd5a570 100644 --- a/inspect-main/inspect-main-task/pom.xml +++ b/inspect-main/inspect-main-task/pom.xml @@ -49,5 +49,19 @@ jackson-core-asl 1.9.13 + + + + net.coobird + thumbnailator + 0.4.19 + + + + + org.apache.poi + poi-ooxml + 5.2.3 + \ No newline at end of file diff --git a/inspect-main/inspect-main-task/src/main/java/com/inspect/task/controller/PatrolTaskController.java b/inspect-main/inspect-main-task/src/main/java/com/inspect/task/controller/PatrolTaskController.java index 0357fd4..9af427e 100644 --- a/inspect-main/inspect-main-task/src/main/java/com/inspect/task/controller/PatrolTaskController.java +++ b/inspect-main/inspect-main-task/src/main/java/com/inspect/task/controller/PatrolTaskController.java @@ -61,6 +61,7 @@ import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; +import net.coobird.thumbnailator.Thumbnails; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -2453,26 +2454,39 @@ public class PatrolTaskController extends BaseController { } } logger.info("图片总数: {}", uniqueImages.size()); - // 并行下载图片 + logger.info("数据处理耗时: {} ms", (System.currentTimeMillis() - start)); + long startDownImg = System.currentTimeMillis(); +// // 并行下载图片 Map streamHashMap = new ConcurrentHashMap<>(); +// uniqueImages.parallelStream().forEach(imagePath -> { +// try { +// sftpClient.downLoad(imagePath, (inputStream) -> { +// byte[] shm = streamHashMap.get(imagePath); +// byte[] bytes = getStringByInputStream(inputStream); +// if (shm == null) { +// streamHashMap.put(imagePath, bytes); +// } +// +// }); +// } catch (Exception e) { +// logger.warn("图片下载失败: {}", imagePath, e); +// } +// }); + // uniqueImages.parallelStream().forEach(imagePath -> { - try { - sftpClient.downLoad(imagePath, (inputStream) -> { - byte[] shm = streamHashMap.get(imagePath); - byte[] bytes = getStringByInputStream(inputStream); - if (shm == null) { - streamHashMap.put(imagePath, bytes); - } - - }); - } catch (Exception e) { - logger.warn("图片下载失败: {}", imagePath, e); + byte[] shm = streamHashMap.get(imagePath); + byte[] bytes = downloadAndCompressImage(imagePath); + if (shm == null) { + streamHashMap.put(imagePath, bytes); } }); logger.info("图片下载成功"); + logger.info("图片下载耗时: {} ms", (System.currentTimeMillis() - startDownImg)); // exportExcel(response, newList); + long startExport = System.currentTimeMillis(); exportExcelStream(response, newList, streamHashMap); - logger.info("任务详情导出耗时: {} ms", (System.currentTimeMillis() - start)); + logger.info("任务详情导出耗时: {} ms", (System.currentTimeMillis() - startExport)); + logger.info("任务详情导出总流程耗时: {} ms", (System.currentTimeMillis() - start)); return ResponseEntity.ok("数据导出成功!"); } @@ -2510,6 +2524,25 @@ public class PatrolTaskController extends BaseController { } } + private byte[] downloadAndCompressImage(String imagePath) { + try { + // 直接下载并压缩图片 + ByteArrayOutputStream output = new ByteArrayOutputStream(); + sftpClient.downLoad(imagePath, inputStream -> { + // 压缩图片 + Thumbnails.of(inputStream) + .size(300, 300) // 限制最大尺寸 + .outputQuality(0.5) // 50%质量 + .outputFormat("JPEG") + .toOutputStream(output); + }); + return output.toByteArray(); + } catch (Exception e) { + logger.warn("图片下载压缩失败: {}", imagePath, e); + return null; + } + } + private void createHeaderRow(Sheet sheet) { String[] headers = { "编号", "设备", "相机名", "系统", "点位名称", "状态", @@ -2556,16 +2589,16 @@ public class PatrolTaskController extends BaseController { try { // 获取第一张初筛图片(如果有) String firstImage = getFirstImage(item.getImg()); - if (firstImage != null && imageCache.containsKey(firstImage)) { - byte[] imageData = imageCache.get(firstImage); + if (firstImage != null) { + byte[] imageData = imageCache.getOrDefault(firstImage, new byte[0]); // 插入筛选图片(第8列) insertImage(workbook, drawing, row, 8, imageData); } // 获取第一张分析图片(如果有) String firstImgAnalyse = getFirstImage(item.getImgAnalyse()); - if (firstImgAnalyse != null && imageCache.containsKey(firstImgAnalyse)) { - byte[] imageData = imageCache.get(firstImgAnalyse); + if (firstImgAnalyse != null) { + byte[] imageData = imageCache.getOrDefault(firstImgAnalyse, new byte[0]); // 插入分析图片(第10列) insertImage(workbook, drawing, row, 10, imageData); }