我有一个Java应用程序,其中我的用例是检测上传的文件类型(.docx、.doc、.ppt、.pptx、.xls、.xlsx)是否受密码保护。
我得到了使用 apache pdfbox 库验证我的 PDF 文件的解决方案
private boolean isPdfPasswordProtected(InputStream inputStream) {
try (PDDocument document = PDDocument.load(inputStream)) {
return document.isEncrypted();
} catch (InvalidPasswordException e) {
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
但是对于这些文件类型(.docx、.doc、.ppt、.pptx、.xls、.xlsx),我得到了可以使用 apache poi-ooxml 的建议,但我提到的实现都不适合我。他们还说 Apache Tika 元数据可用于识别是否加密,但这对我来说也不起作用。请帮助我验证文件是否加密。
我尝试使用 Apache POI 和以下代码,但它不适用于加密的 Word 文档:
private boolean isWordPasswordProtected(InputStream inputStream, String contentType) {
try {
if (contentType.equalsIgnoreCase("application/msword")) { // Check for DOC files first
try (POIFSFileSystem poifs = new POIFSFileSystem(inputStream)) {
HWPFDocument doc = new HWPFDocument(poifs);
// Accessing properties will trigger password check
doc.getSummaryInformation();
return false; // Not password protected
} catch (EncryptedDocumentException e) {
return true; // Password protected
}
} else if (contentType.equalsIgnoreCase("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) { // Then handle DOCX files
try (XWPFDocument docx = new XWPFDocument(inputStream)) {
// Accessing properties will trigger password check
docx.getProperties().getCoreProperties().getTitle();
return false; // Not password protected
} catch (EncryptedDocumentException e) {
return true; // Password protected
}
} else {
// Handle unsupported file formats
throw new IllegalArgumentException("Unsupported file format: " + contentType);
}
} catch (Exception e) {
// Handle exceptions
e.printStackTrace();
throw new RuntimeException("Error checking password protection", e);
}
}
我尝试使用 apache tika 来验证元数据,但没有成功。请找到以下代码:
private boolean isWordPasswordProtected(InputStream inputStream, String contentType) {
Metadata metadata = new Metadata();
Parser parser = new AutoDetectParser();
// To extract metadata only, we use BodyContentHandler with -1 as the maximum characters
BodyContentHandler handler = new BodyContentHandler(1000);
ParseContext context = new ParseContext();
context.set(Parser.class, parser);
try {
parser.parse(inputStream, handler, metadata, context);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (TikaException e) {
throw new RuntimeException(e);
}
// Check metadata for encryption-related information
String encryption = metadata.get("encryption");
return encryption != null && !encryption.isEmpty();
}
请帮助我实施这一点。
对于 Excel 文件,您可以尝试以下操作:
private Boolean checkExcelFileisEncrypted(MultipartFile file) throws IOException {
try (InputStream inputStream = file.getInputStream()) {
WorkbookFactory.create(inputStream);
return Boolean.FALSE;
} catch (EncryptedDocumentException e) {
return Boolean.TRUE;
}
}