Merge pull request #400 from gitchenjh/master

修复升级poi版本后判断office是否受密码保护方法
pull/56/head
gitchenjh 2022-11-11 10:53:33 +08:00 committed by GitHub
commit 8fb32e4f73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 8 deletions

View File

@ -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";
/**
* officeword,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;
}