From da2330bb7951c602ebe72a9f53ff13d0895b3349 Mon Sep 17 00:00:00 2001 From: jinlin <jinlin> Date: 星期五, 15 十二月 2023 10:57:55 +0800 Subject: [PATCH] 修改 --- core/src/main/java/com/zt/life/sys/service/SysOssConfigService.java | 78 +++++++++++++++++++++++++++++++++++++++ 1 files changed, 78 insertions(+), 0 deletions(-) diff --git a/core/src/main/java/com/zt/life/sys/service/SysOssConfigService.java b/core/src/main/java/com/zt/life/sys/service/SysOssConfigService.java index 15dd4b2..f782b58 100644 --- a/core/src/main/java/com/zt/life/sys/service/SysOssConfigService.java +++ b/core/src/main/java/com/zt/life/sys/service/SysOssConfigService.java @@ -13,16 +13,27 @@ import com.zt.core.oss.service.ISysOssConfigService; import com.zt.core.oss.service.ISysOssService; import com.zt.life.core.constant.Cache; +import com.zt.life.export.service.DownloadService; +import com.zt.life.oss.OssEncryptService; import com.zt.life.sys.dao.SysOssConfigDao; import com.zt.life.sys.dto.OssDto; import com.zt.life.sys.mapstruct.OssExMapper; import com.zt.life.sys.model.SysOssConfig; +import com.zt.life.util.ZipCipherUtil; import com.zt.modules.oss.model.SysOss; import com.zt.modules.oss.service.SysOssService; +import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CacheEvict; import org.springframework.stereotype.Service; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.*; import java.util.stream.Collectors; @@ -42,6 +53,15 @@ @Autowired private SysOssService sysOssServices; + + @Value("${zt.oss.local-path}") + private String localPath; + + @Value("${data.key}") + private String key; + + @Autowired + private OssEncryptService ossEncryptService; /** * 鍒嗛〉鏌ヨ @@ -320,4 +340,62 @@ return baseDao.selectList(new QueryWrapper<SysOssConfig>().eq(Constant.TableColumn.IS_DELETE, Constant.Bool.NO).lambda() .eq(SysOssConfig::getBusiType, busiType)); } + + public void downloadFilesByosList(HttpServletRequest request, HttpServletResponse response, List<SysOss> list, String projectName) { + try { + String uuid = UUIDUtil.getUUID(); + String remoteFileFullPath = Paths.get(localPath, "temp", uuid).toString(); + Files.createDirectories(Paths.get(remoteFileFullPath)); + + for (SysOss so : list) { + try { + String localFileFullPath = Paths.get(localPath, so.getPath()).toString(); + File localFile = new File(localFileFullPath); + if (localFile.exists()) { + try (InputStream in = ossEncryptService.decrypt(localFile); + BufferedInputStream bis = new BufferedInputStream(in); + BufferedOutputStream bos = new BufferedOutputStream( + new FileOutputStream(remoteFileFullPath + File.separator + so.getName()))) { + + byte[] buffer = new byte[1024]; + int len; + while ((len = bis.read(buffer)) != -1) { + bos.write(buffer, 0, len); + } + } + } + } catch (Exception e) { + e.printStackTrace(); // 鎴栬�呰褰曟棩蹇� + } + } + + String fileNameZip = Paths.get(localPath, "temp", uuid + ".zip").toString(); + new ZipCipherUtil().encryptZip(remoteFileFullPath, fileNameZip, key); + + File zipFile = new File(fileNameZip); + response.reset(); + response.setContentLength((int) zipFile.length()); + response.setContentType("application/octet-stream;charset=UTF-8"); + String encodedFilename = DownloadService.getNameEncoder(request, projectName + ".zip"); + response.addHeader("Content-Disposition", "attachment;filename=" + encodedFilename); + + try (FileInputStream fileInputStream = new FileInputStream(zipFile); + ServletOutputStream outputStream = response.getOutputStream()) { + + byte[] buffers = new byte[1024]; + int length; + while ((length = fileInputStream.read(buffers)) > 0) { + outputStream.write(buffers, 0, length); + } + + } catch (IOException e) { + e.printStackTrace(); // 鎴栬�呰褰曟棩蹇� + } finally { + FileUtils.deleteQuietly(new File(remoteFileFullPath)); + FileUtils.deleteQuietly(new File(fileNameZip));// 鍒犻櫎涓存椂鏂囦欢澶瑰強鍏跺唴瀹� + } + } catch (Exception ex) { + ex.printStackTrace(); // 鎴栬�呰褰曟棩蹇� + } + } } -- Gitblit v1.9.1