From baeb078d43b2d3e980db66e3e037e185bbc671f3 Mon Sep 17 00:00:00 2001 From: caihaohan Date: Wed, 28 Feb 2024 14:04:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8E=8B=E7=BC=A9=E5=8C=85=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/CommonController.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java b/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java index 02152f4a..5083d134 100644 --- a/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java +++ b/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java @@ -1,5 +1,6 @@ package com.jero.modules.system.controller; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; @@ -33,6 +34,14 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLDecoder; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + /** *

* 用户表 前端控制器 @@ -277,6 +286,80 @@ public class CommonController { } + /** + * 预览图片&下载文件 + * + * @param map 传入文件ids + * @param response HttpServletResponse + */ + @PostMapping(value = "/zipDownload") + public void zipDownload(@RequestBody Map map, HttpServletResponse response) { + String ids = map.get("ids"); + if (StrUtil.isBlank(ids)) { + throw new JeroBootException("ids不能为空"); + } + List idList = Arrays.asList(ids.split(",")); + + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.in(OSSFile::getId, idList); + List ossFileList = ossFileService.list(queryWrapper); + if (ossFileList == null || ossFileList.isEmpty() || ossFileList.size() != idList.size()) { + throw new JeroBootException("实际存在文件数量与预期数量不匹配"); + } + + response.setContentType("application/zip"); + response.setStatus(HttpServletResponse.SC_OK); + response.addHeader("Content-Disposition", "attachment; filename=\"files.zip\""); + + try (ZipOutputStream zippedOut = new ZipOutputStream(response.getOutputStream())) { + for (OSSFile ossFile : ossFileList) { + String fileUrl = ossFile.getUrl(); + String fileName = ossFile.getFileName(); // Assume fileName is set correctly in OSSFile + InputStream inputStream = null; + + if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) { + String filePath = uploadpath + File.separator + fileUrl; + File file = new File(filePath); + if (!file.exists()) { + throw new RuntimeException("文件不存在: " + filePath); + } + inputStream = new BufferedInputStream(Files.newInputStream(Paths.get(filePath))); + } else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { + String minioUrl = MinioUtil.getMinioUrl() + MinioUtil.getBucketName() + "/"; + String url = fileUrl.replace(minioUrl, ""); + inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url); + } + + // Add ZIP entry to output stream. + zippedOut.putNextEntry(new ZipEntry(fileName)); + + // Transfer bytes from the file to the ZIP file + byte[] buf = new byte[1024]; + int len; + while ((len = inputStream.read(buf)) > 0) { + zippedOut.write(buf, 0, len); + } + + // Complete the entry + zippedOut.closeEntry(); + inputStream.close(); + + Integer downloadNumber = ossFile.getDownloadNumber(); + if (downloadNumber == null) { + ossFile.setDownloadNumber(0); + } else { + ossFile.setDownloadNumber(downloadNumber + 1); + } + ossFileService.saveDownloadTimes(ossFile.getId(), null); + ossFileService.updateAndPush(ossFile); + } + } catch (IOException e) { + log.error("文件打包失败" + e.getMessage()); + response.setStatus(404); + } + } + + /** * 预览图片&下载文件 *