如何检测文件格式并检查文件是否已加密
Contents
[
Hide
]
有时,在打开文件之前需要检测文件的格式,因为文件扩展名不能保证文件内容是适当的。文件可能已加密(受密码保护的文件),因此无法直接读取,或者我们不应该读取。Aspose.Cells提供了FileFormatUtil.detectFileFormat()静态方法和一些相关的API,供您用于处理文档。
Java 代码以检测文件格式并检查文件是否已加密
以下示例代码说明如何通过文件路径检测文件格式并检查其扩展名。您还可以确定该文件是否已加密。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DetectFileFormatandCheckFileEncrypted.class); | |
// Detect file format | |
FileFormatInfo info = FileFormatUtil.detectFileFormat(dataDir + "Book1.xlsx"); | |
// Gets the detected load format | |
System.out.println("The spreadsheet format is: " + FileFormatUtil.loadFormatToExtension(info.getLoadFormat())); | |
// Check if the file is encrypted. | |
System.out.println("The file is encrypted: " + info.isEncrypted()); |