From 04401ee6002298fdf469d4320902ca560b95a49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=B2=BE=E5=8D=8E?= <842761733@qq.com> Date: Wed, 28 Dec 2022 13:52:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Doffice=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=AF=86=E7=A0=81=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/keking/utils/OfficeUtils.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/cn/keking/utils/OfficeUtils.java b/server/src/main/java/cn/keking/utils/OfficeUtils.java index 0131178d..fd05fe03 100644 --- a/server/src/main/java/cn/keking/utils/OfficeUtils.java +++ b/server/src/main/java/cn/keking/utils/OfficeUtils.java @@ -1,6 +1,7 @@ package cn.keking.utils; import org.apache.commons.lang3.exception.ExceptionUtils; +import org.apache.poi.EncryptedDocumentException; import org.apache.poi.extractor.ExtractorFactory; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.springframework.lang.Nullable; @@ -17,7 +18,7 @@ import java.nio.file.Paths; */ public class OfficeUtils { - private static final String POI_INVALID_PASSWORD_MSG = "Invalid password specified"; + private static final String POI_INVALID_PASSWORD_MSG = "password"; /** * 判断office(word,excel,ppt)文件是否受密码保护 @@ -28,15 +29,17 @@ public class OfficeUtils { public static boolean isPwdProtected(String path) { try { ExtractorFactory.createExtractor(Files.newInputStream(Paths.get(path))); - } catch (IOException e) { - if (POI_INVALID_PASSWORD_MSG.equals(e.getMessage())) { + } catch (IOException | EncryptedDocumentException e) { + if (e.getMessage().toLowerCase().contains(POI_INVALID_PASSWORD_MSG)) { return true; } } catch (Exception e) { Throwable[] throwableArray = ExceptionUtils.getThrowables(e); for (Throwable throwable : throwableArray) { - if (throwable instanceof IOException && POI_INVALID_PASSWORD_MSG.equals(throwable.getMessage())) { - return true; + if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) { + if (e.getMessage().toLowerCase().contains(POI_INVALID_PASSWORD_MSG)) { + return true; + } } } }