From 9cd53a57b1fa944444b5a6e7dfbb4e8c99f32fca Mon Sep 17 00:00:00 2001 From: lijiarao Date: Mon, 25 Mar 2024 11:57:57 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=96=87=E6=A1=A3=E6=8B=86=E5=88=86?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E8=A7=A3=E5=AF=86=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jero/common/util}/IntekeyUtils.java | 40 +++++++++++++++---- .../download/controller/SearchController.java | 4 +- .../impl/DownloadDecryptFileServiceImpl.java | 21 ++++------ .../split/service/impl/FileSpiltService.java | 20 +++++++--- 4 files changed, 56 insertions(+), 29 deletions(-) rename {laws-modules-docking/src/main/java/com/jero/modules/docking/utils => laws-base/laws-base-core/src/main/java/com/jero/common/util}/IntekeyUtils.java (76%) diff --git a/laws-modules-docking/src/main/java/com/jero/modules/docking/utils/IntekeyUtils.java b/laws-base/laws-base-core/src/main/java/com/jero/common/util/IntekeyUtils.java similarity index 76% rename from laws-modules-docking/src/main/java/com/jero/modules/docking/utils/IntekeyUtils.java rename to laws-base/laws-base-core/src/main/java/com/jero/common/util/IntekeyUtils.java index d5194926..5c820d1e 100644 --- a/laws-modules-docking/src/main/java/com/jero/modules/docking/utils/IntekeyUtils.java +++ b/laws-base/laws-base-core/src/main/java/com/jero/common/util/IntekeyUtils.java @@ -1,16 +1,15 @@ -package com.jero.modules.docking.utils; +package com.jero.common.util; import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.http.*; +import org.springframework.stereotype.Component; import org.springframework.util.DigestUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; @@ -23,9 +22,29 @@ import java.util.Objects; * intekey解密文件相关 */ @Slf4j +@Component public class IntekeyUtils { + public static boolean ENABLE; + public static String DECRYPT_URL; + public static String DECRYPT_APP_CODE; + public static String DECRYPT_SECRET_KEY; - private static final Logger logger = LoggerFactory.getLogger(IntekeyUtils.class); + @Value("${download.enable}") + public void setEnable(boolean enable) { + ENABLE = enable; + } + @Value("${download.decrypt.url}") + public void setDecryptUrl(String decryptUrl) { + DECRYPT_URL = decryptUrl; + } + @Value("${download.decrypt.app_code}") + public void setDecryptAppCode(String decryptAppCode) { + DECRYPT_APP_CODE = decryptAppCode; + } + @Value("${download.decrypt.secret_key}") + public void setDecryptSecretKey(String decryptSecretKey) { + DECRYPT_SECRET_KEY = decryptSecretKey; + } /** * 解密文件,返回InputStream,供excel使用 @@ -35,10 +54,9 @@ public class IntekeyUtils { * @param multipartFile * @return */ - //todo 这个方法需要写在laws-modules-docking 里面 可以新建一个文件夹是encrypt - public static InputStream DecryptFile(String url, String appCode, String secretKey, Integer scope, MultipartFile multipartFile) { + public static InputStream decryptFile(String url, String appCode, String secretKey, Integer scope, MultipartFile multipartFile) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); - logger.info("解密时间:" + format.format(new Date())); + log.info("解密时间:" + format.format(new Date())); //设置校验 Long time = System.currentTimeMillis(); @@ -92,6 +110,12 @@ public class IntekeyUtils { return file; } + public static InputStream getInputStreamByDecryptFile(MultipartFile file) { + if(ENABLE){ + return decryptFile(DECRYPT_URL, DECRYPT_APP_CODE, DECRYPT_SECRET_KEY, null, file); + } + return null; + } public static void main(String args[]) { String originalFilename = "C:\\Users\\LQT\\Desktop\\参考代码 - 副本.txt"; diff --git a/laws-modules-docking/src/main/java/com/jero/modules/docking/download/controller/SearchController.java b/laws-modules-docking/src/main/java/com/jero/modules/docking/download/controller/SearchController.java index ab63b6ba..53af8e60 100644 --- a/laws-modules-docking/src/main/java/com/jero/modules/docking/download/controller/SearchController.java +++ b/laws-modules-docking/src/main/java/com/jero/modules/docking/download/controller/SearchController.java @@ -1,6 +1,6 @@ package com.jero.modules.docking.download.controller; -import com.jero.modules.docking.utils.IntekeyUtils; +import com.jero.common.util.IntekeyUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -28,7 +28,7 @@ public class SearchController { @RequestParam("secretKey") String secretKey, @RequestParam(value = "scope",required = false) Integer scope, HttpServletResponse response) { - try (InputStream inputStream = IntekeyUtils.DecryptFile(url, appCode, secretKey, scope, file); + try (InputStream inputStream = IntekeyUtils.decryptFile(url, appCode, secretKey, scope, file); OutputStream outputStream = response.getOutputStream() ) { byte[] buf = new byte[1024]; diff --git a/laws-modules-docking/src/main/java/com/jero/modules/docking/download/service/impl/DownloadDecryptFileServiceImpl.java b/laws-modules-docking/src/main/java/com/jero/modules/docking/download/service/impl/DownloadDecryptFileServiceImpl.java index a011a5df..ebeef32c 100644 --- a/laws-modules-docking/src/main/java/com/jero/modules/docking/download/service/impl/DownloadDecryptFileServiceImpl.java +++ b/laws-modules-docking/src/main/java/com/jero/modules/docking/download/service/impl/DownloadDecryptFileServiceImpl.java @@ -1,32 +1,23 @@ package com.jero.modules.docking.download.service.impl; -import com.jero.common.api.vo.ResultCommon; -import com.jero.common.exception.JeroBootException; -import com.jero.common.system.vo.LoginUser; import com.jero.modules.docking.download.config.ContentCachingWrapperFilter; import com.jero.modules.docking.download.service.DownloadDecryptFileService; -import com.jero.modules.docking.utils.IntekeyUtils; -import lombok.SneakyThrows; +import com.jero.common.util.IntekeyUtils; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.entity.ContentType; -import org.apache.shiro.SecurityUtils; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Service; import org.springframework.web.method.HandlerMethod; import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.util.ContentCachingResponseWrapper; -import org.springframework.web.util.WebUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.UUID; @@ -151,7 +142,7 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic decryptFile(response, file, null); }else if(b1){ // 下载文件 先解密,判断组织域,加密 - try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)) { + try (InputStream is = IntekeyUtils.decryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)) { MultipartFile fileDecrypt = getMultipartFile(response,is, contentType, fileName); // 判断fileDecrypt是不是zip压缩包,如果是压缩包的话则不加密 boolean isZipFile = false; @@ -159,7 +150,9 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic if (originalFileName != null && originalFileName.endsWith(".zip")) { isZipFile = true; } - if (isZipFile) return; + if (isZipFile) { + return; + } // 加密 encryptFile(response, fileDecrypt, scope); } catch (Exception e) { @@ -197,7 +190,7 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic } private void decryptFile(HttpServletResponse response, MultipartFile file, Integer scope) { - try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, scope, file)) { + try (InputStream is = IntekeyUtils.decryptFile(decryptUrl, decryptAppCode, decryptSecretKey, scope, file)) { writeResponse(response, is); } catch (Exception e) { log.error("Exception:",e); @@ -206,7 +199,7 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic } private void encryptFile(HttpServletResponse response, MultipartFile file, Integer scope) { - try (InputStream is = IntekeyUtils.DecryptFile(encryptUrl, encryptAppCode, encryptSecretKey, scope, file)) { + try (InputStream is = IntekeyUtils.decryptFile(encryptUrl, encryptAppCode, encryptSecretKey, scope, file)) { writeResponse(response, is); } catch (Exception e) { log.error("Exception:",e); diff --git a/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java b/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java index 2ec4a387..ce61fe92 100644 --- a/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java +++ b/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java @@ -6,6 +6,7 @@ import cn.hutool.http.HttpUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.jero.common.exception.JeroBootException; import com.jero.common.system.vo.LoginUser; +import com.jero.common.util.IntekeyUtils; import com.jero.common.util.MinioUtil; import com.jero.common.util.UUIDUtils; import com.jero.modules.laws.documenttool.mapper.DocumentSplitMapper; @@ -26,6 +27,7 @@ import com.jero.modules.split.vo.TitleNumberVO; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.http.entity.ContentType; import org.apache.poi.hwpf.usermodel.PictureType; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.util.ZipSecureFile; @@ -45,13 +47,16 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import javax.annotation.Resource; import java.io.IOException; import java.io.InputStream; import java.util.*; @@ -161,7 +166,7 @@ public class FileSpiltService { // } // 获取文件地址 String fileId = sarFileSplitInfoEO.getFileId(); - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(OSSFile::getId, fileId); queryWrapper.orderByDesc(OSSFile::getCreateTime); List ossFileList = iossFileService.list(queryWrapper); @@ -174,14 +179,19 @@ public class FileSpiltService { throw new JeroBootException("当前文件未找到,请重新选择!"); } try (InputStream is = MinioUtil.download(readFilePath);InputStream is1 = MinioUtil.download(readFilePath)) { - //String readFilePath = "E:\\Data\\Downloads\\wordtest\\GB7258标准修订test - 表格测试 - 副本.docx"; - //try (InputStream is = new FileInputStream(readFilePath); InputStream is1 = new FileInputStream(readFilePath)) {//需要将文件路更改为word文档所在路径。 + String fileName = readFilePath.substring((readFilePath.lastIndexOf("/") + 1)); + MultipartFile mFile = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), is); + MultipartFile mFile1 = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), is1); + InputStream inputStream = IntekeyUtils.getInputStreamByDecryptFile(mFile); + InputStream inputStream1 = IntekeyUtils.getInputStreamByDecryptFile(mFile1); + inputStream = inputStream == null ? is : inputStream; + inputStream1 = inputStream1 == null ? is1 : inputStream1; ZipSecureFile.setMinInflateRatio(-1.0d); - XWPFDocument doc = new XWPFDocument(is); + XWPFDocument doc = new XWPFDocument(inputStream); // 获取文档中的图片 List allPictures = doc.getAllPictures(); //获得序号标题和无标题序号 - TitleNumberVO titleNumberVO = AsposeUtil.getNumber(is1); + TitleNumberVO titleNumberVO = AsposeUtil.getNumber(inputStream1); //TitleNumberVO titleNumberVO = new TitleNumberVO(); Map titleNumberMap = titleNumberVO.getTitleNumberMap(); List emptyTitleList = titleNumberVO.getEmptyTitleList();