修复office文件密码判断
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package cn.keking.utils;
|
package cn.keking.utils;
|
||||||
|
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
|
import org.apache.poi.EncryptedDocumentException;
|
||||||
import org.apache.poi.extractor.ExtractorFactory;
|
import org.apache.poi.extractor.ExtractorFactory;
|
||||||
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
|
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
@ -17,7 +18,7 @@ import java.nio.file.Paths;
|
|||||||
*/
|
*/
|
||||||
public class OfficeUtils {
|
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)文件是否受密码保护
|
* 判断office(word,excel,ppt)文件是否受密码保护
|
||||||
@ -28,15 +29,17 @@ public class OfficeUtils {
|
|||||||
public static boolean isPwdProtected(String path) {
|
public static boolean isPwdProtected(String path) {
|
||||||
try {
|
try {
|
||||||
ExtractorFactory.createExtractor(Files.newInputStream(Paths.get(path)));
|
ExtractorFactory.createExtractor(Files.newInputStream(Paths.get(path)));
|
||||||
} catch (IOException e) {
|
} catch (IOException | EncryptedDocumentException e) {
|
||||||
if (POI_INVALID_PASSWORD_MSG.equals(e.getMessage())) {
|
if (e.getMessage().toLowerCase().contains(POI_INVALID_PASSWORD_MSG)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
||||||
for (Throwable throwable : throwableArray) {
|
for (Throwable throwable : throwableArray) {
|
||||||
if (throwable instanceof IOException && POI_INVALID_PASSWORD_MSG.equals(throwable.getMessage())) {
|
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
|
||||||
return true;
|
if (e.getMessage().toLowerCase().contains(POI_INVALID_PASSWORD_MSG)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user