修复office文件密码判断
parent
2184264831
commit
e0cc2b6a01
|
@ -1,12 +1,13 @@
|
||||||
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;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Office工具类
|
* Office工具类
|
||||||
|
@ -16,6 +17,8 @@ import java.io.FileInputStream;
|
||||||
*/
|
*/
|
||||||
public class OfficeUtils {
|
public class OfficeUtils {
|
||||||
|
|
||||||
|
private static final String POI_INVALID_PASSWORD_MSG = "Invalid password specified";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断office(word,excel,ppt)文件是否受密码保护
|
* 判断office(word,excel,ppt)文件是否受密码保护
|
||||||
*
|
*
|
||||||
|
@ -24,13 +27,15 @@ public class OfficeUtils {
|
||||||
*/
|
*/
|
||||||
public static boolean isPwdProtected(String path) {
|
public static boolean isPwdProtected(String path) {
|
||||||
try {
|
try {
|
||||||
ExtractorFactory.createExtractor(new FileInputStream(path));
|
ExtractorFactory.createExtractor(Files.newInputStream(Paths.get(path)));
|
||||||
} catch (EncryptedDocumentException e) {
|
} catch (IOException e) {
|
||||||
|
if (POI_INVALID_PASSWORD_MSG.equals(e.getMessage())) {
|
||||||
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 EncryptedDocumentException) {
|
if (throwable instanceof IOException && POI_INVALID_PASSWORD_MSG.equals(throwable.getMessage())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +53,7 @@ public class OfficeUtils {
|
||||||
public static synchronized boolean isCompatible(String path, @Nullable String password) {
|
public static synchronized boolean isCompatible(String path, @Nullable String password) {
|
||||||
try {
|
try {
|
||||||
Biff8EncryptionKey.setCurrentUserPassword(password);
|
Biff8EncryptionKey.setCurrentUserPassword(password);
|
||||||
ExtractorFactory.createExtractor(new FileInputStream(path));
|
ExtractorFactory.createExtractor(Files.newInputStream(Paths.get(path)));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
|
|
Loading…
Reference in New Issue