|
|
@@ -7,6 +7,7 @@ import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
|
|
|
import org.springframework.lang.Nullable;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
|
@@ -27,8 +28,10 @@ public class OfficeUtils {
|
|
|
* @return 是否受密码保护
|
|
|
*/
|
|
|
public static boolean isPwdProtected(String path) {
|
|
|
+ InputStream propStream = null;
|
|
|
try {
|
|
|
- ExtractorFactory.createExtractor(Files.newInputStream(Paths.get(path)));
|
|
|
+ propStream = Files.newInputStream(Paths.get(path));
|
|
|
+ ExtractorFactory.createExtractor(propStream);
|
|
|
} catch (IOException | EncryptedDocumentException e) {
|
|
|
if (e.getMessage().toLowerCase().contains(POI_INVALID_PASSWORD_MSG)) {
|
|
|
return true;
|
|
|
@@ -42,10 +45,17 @@ public class OfficeUtils {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ }finally {
|
|
|
+ if(propStream!=null) {//如果文件输入流不是null
|
|
|
+ try {
|
|
|
+ propStream.close();//关闭文件输入流
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 判断office文件是否可打开(兼容)
|
|
|
*
|
|
|
@@ -54,15 +64,24 @@ public class OfficeUtils {
|
|
|
* @return 是否可打开(兼容)
|
|
|
*/
|
|
|
public static synchronized boolean isCompatible(String path, @Nullable String password) {
|
|
|
+ InputStream propStream = null;
|
|
|
try {
|
|
|
+ propStream = Files.newInputStream(Paths.get(path));
|
|
|
Biff8EncryptionKey.setCurrentUserPassword(password);
|
|
|
- ExtractorFactory.createExtractor(Files.newInputStream(Paths.get(path)));
|
|
|
+ ExtractorFactory.createExtractor(propStream);
|
|
|
} catch (Exception e) {
|
|
|
return false;
|
|
|
} finally {
|
|
|
Biff8EncryptionKey.setCurrentUserPassword(null);
|
|
|
+ if(propStream!=null) {//如果文件输入流不是null
|
|
|
+ try {
|
|
|
+ propStream.close();//关闭文件输入流
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+}
|