From 75e568e15faf2d5136f64374f71715a8c329b1f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=9B=84?= Date: Sat, 8 Apr 2023 05:45:17 +0000 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E5=8C=85=E5=AF=86=E7=A0=81=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=8E=8B=E7=BC=A9=E5=8C=85LINUX=E4=B8=8B=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E4=B9=B1=E7=A0=81=20=E8=A7=A3=E5=8E=8B=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=96=87=E4=BB=B6=E7=9B=AE=E5=BD=95=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20=E6=94=AF=E6=8C=81=E5=8E=8B=E7=BC=A9=E5=8C=85?= =?UTF-8?q?=E5=AF=86=E7=A0=81=20=E4=BF=AE=E5=A4=8D=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E5=8C=85LINUX=E4=B8=8B=E4=B8=AD=E6=96=87=E4=B9=B1=E7=A0=81=20?= =?UTF-8?q?=E8=A7=A3=E5=8E=8B=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 高雄 --- .../service/impl/CompressFilePreviewImpl.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/server/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java b/server/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java index 36defd00..e2fc3376 100644 --- a/server/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java +++ b/server/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java @@ -7,11 +7,15 @@ import cn.keking.service.FilePreview; import cn.keking.utils.DownloadUtils; import cn.keking.service.FileHandlerService; import cn.keking.service.CompressFileReader; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.apache.poi.EncryptedDocumentException; import org.springframework.stereotype.Service; import org.springframework.ui.Model; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; +import java.io.IOException; + /** * Created by kl on 2018/1/17. * Content :处理压缩包文件 @@ -22,6 +26,7 @@ public class CompressFilePreviewImpl implements FilePreview { private final FileHandlerService fileHandlerService; private final CompressFileReader compressFileReader; private final OtherFilePreviewImpl otherFilePreview; + private static final String Rar_PASSWORD_MSG = "password"; public CompressFilePreviewImpl(FileHandlerService fileHandlerService, CompressFileReader compressFileReader, OtherFilePreviewImpl otherFilePreview) { this.fileHandlerService = fileHandlerService; this.compressFileReader = compressFileReader; @@ -32,7 +37,7 @@ public class CompressFilePreviewImpl implements FilePreview { public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) { String fileName=fileAttribute.getName(); String filePassword = fileAttribute.getFilePassword(); - String fileTree; + String fileTree = null; // 判断文件名是否存在(redis缓存读取) if (!StringUtils.hasText(fileHandlerService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) { ReturnResponse response = DownloadUtils.downLoad(fileAttribute, fileName); @@ -40,29 +45,32 @@ public class CompressFilePreviewImpl implements FilePreview { return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg()); } String filePath = response.getContent(); - fileTree = compressFileReader.unRar(filePath, filePassword,fileName); - if ("Password".equals(fileTree)) { - model.addAttribute("needFilePassword", true); - return EXEL_FILE_PREVIEW_PAGE; - } - if ("error".equals(fileTree) ) { - return otherFilePreview.notSupportedFile(model, fileAttribute, "解压失败:密码错误或者其他错误...."); + try { + fileTree = compressFileReader.unRar(filePath, filePassword,fileName); + } catch (Exception e) { + Throwable[] throwableArray = ExceptionUtils.getThrowables(e); + for (Throwable throwable : throwableArray) { + if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) { + if (e.getMessage().toLowerCase().contains(Rar_PASSWORD_MSG)) { + model.addAttribute("needFilePassword", true); + return EXEL_FILE_PREVIEW_PAGE; + } + } + } } if (!ObjectUtils.isEmpty(fileTree)) { if (ConfigConstants.isCacheEnabled()) { // 加入缓存 fileHandlerService.addConvertedFile(fileName, fileTree); } + }else { + return otherFilePreview.notSupportedFile(model, fileAttribute, "压缩文件密码错误! 压缩文件损坏! 压缩文件类型不受支持!"); } } else { fileTree = fileHandlerService.getConvertedFile(fileName); } - if (!ObjectUtils.isEmpty(fileTree)) { model.addAttribute("fileName", fileName); model.addAttribute("fileTree", fileTree); return COMPRESS_FILE_PREVIEW_PAGE; - } else { - return otherFilePreview.notSupportedFile(model, fileAttribute, "压缩文件类型不受支持"); - } } }