修复office文件密码判断

pull/63/head
陈精华 2 years ago
parent e0cc2b6a01
commit 04401ee600
No known key found for this signature in database
GPG Key ID: 30BDC970902B755D

@ -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";
/**
* officeword,excel,ppt
@ -28,18 +29,20 @@ 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())) {
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
if (e.getMessage().toLowerCase().contains(POI_INVALID_PASSWORD_MSG)) {
return true;
}
}
}
}
return false;
}

Loading…
Cancel
Save