mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2025-10-21 02:57:30 +08:00
Merge pull request #400 from gitchenjh/master
修复升级poi版本后判断office是否受密码保护方法
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
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.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Office工具类
|
* Office工具类
|
||||||
@@ -16,6 +16,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)文件是否受密码保护
|
||||||
*
|
*
|
||||||
@@ -25,17 +27,18 @@ public class OfficeUtils {
|
|||||||
public static boolean isPwdProtected(String path) {
|
public static boolean isPwdProtected(String path) {
|
||||||
try {
|
try {
|
||||||
ExtractorFactory.createExtractor(new FileInputStream(path));
|
ExtractorFactory.createExtractor(new FileInputStream(path));
|
||||||
} catch (EncryptedDocumentException e) {
|
} catch (IOException e) {
|
||||||
return true;
|
if (POI_INVALID_PASSWORD_MSG.equals(e.getMessage())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Throwable[] throwables = ExceptionUtils.getThrowables(e);
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
||||||
for (Throwable throwable : throwables) {
|
for (Throwable throwable : throwableArray) {
|
||||||
if (throwable instanceof EncryptedDocumentException) {
|
if (throwable instanceof IOException && POI_INVALID_PASSWORD_MSG.equals(throwable.getMessage())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +58,6 @@ public class OfficeUtils {
|
|||||||
} finally {
|
} finally {
|
||||||
Biff8EncryptionKey.setCurrentUserPassword(null);
|
Biff8EncryptionKey.setCurrentUserPassword(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user