From 764ea702d00c227c8d189d5c955b8ec644976ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=B2=BE=E5=8D=8E?= <842761733@qq.com> Date: Thu, 10 Nov 2022 18:27:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=87=E7=BA=A7poi?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=90=8E=E5=88=A4=E6=96=ADoffice=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=8F=97=E5=AF=86=E7=A0=81=E4=BF=9D=E6=8A=A4=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/keking/utils/OfficeUtils.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/cn/keking/utils/OfficeUtils.java b/server/src/main/java/cn/keking/utils/OfficeUtils.java index 8e9aa3d8..c782b6cf 100644 --- a/server/src/main/java/cn/keking/utils/OfficeUtils.java +++ b/server/src/main/java/cn/keking/utils/OfficeUtils.java @@ -1,12 +1,12 @@ 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; import java.io.FileInputStream; +import java.io.IOException; /** * Office工具类 @@ -16,6 +16,8 @@ import java.io.FileInputStream; */ public class OfficeUtils { + private static final String POI_INVALID_PASSWORD_MSG = "Invalid password specified"; + /** * 判断office(word,excel,ppt)文件是否受密码保护 * @@ -25,17 +27,18 @@ public class OfficeUtils { public static boolean isPwdProtected(String path) { try { ExtractorFactory.createExtractor(new FileInputStream(path)); - } catch (EncryptedDocumentException e) { - return true; + } catch (IOException e) { + if (POI_INVALID_PASSWORD_MSG.equals(e.getMessage())) { + return true; + } } catch (Exception e) { - Throwable[] throwables = ExceptionUtils.getThrowables(e); - for (Throwable throwable : throwables) { - if (throwable instanceof EncryptedDocumentException) { + Throwable[] throwableArray = ExceptionUtils.getThrowables(e); + for (Throwable throwable : throwableArray) { + if (throwable instanceof IOException && POI_INVALID_PASSWORD_MSG.equals(throwable.getMessage())) { return true; } } } - return false; } @@ -55,7 +58,6 @@ public class OfficeUtils { } finally { Biff8EncryptionKey.setCurrentUserPassword(null); } - return true; }