jodconverter转换带密码office文件

This commit is contained in:
陈精华
2022-12-19 16:38:50 +08:00
parent d761d0cc88
commit f52169ec27
5 changed files with 93 additions and 17 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,8 +16,6 @@ import java.io.IOException;
*/
public class OfficeUtils {
private static final String POI_INVALID_PASSWORD_MSG = "Invalid password specified";
/**
* 判断officeword,excel,ppt文件是否受密码保护
*
@@ -27,14 +25,12 @@ public class OfficeUtils {
public static boolean isPwdProtected(String path) {
try {
ExtractorFactory.createExtractor(new FileInputStream(path));
} catch (IOException e) {
if (POI_INVALID_PASSWORD_MSG.equals(e.getMessage())) {
return true;
}
} catch (EncryptedDocumentException e) {
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 EncryptedDocumentException) {
return true;
}
}