如何在Java中实施MS Word的密码保护?
我确实看到了各种加强保护的选项,但没有一个是使用密码的。我该怎么做如何在Java中实施MS Word的密码保护?,java,apache-poi,docx4j,Java,Apache Poi,Docx4j,我确实看到了各种加强保护的选项,但没有一个是使用密码的。我该怎么做 File file = new File(fileName); FileInputStream fis = new FileInputStream(file.getAbsolutePath()); XWPFDocument document = new XWPFDocument(fis); document.enforceCommentsProtection(); document.en
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
document.enforceCommentsProtection();
document.enforceFillingFormsProtection();
document.enforceReadonlyProtection();
document.enforceTrackedChangesProtection();
document.enforceUpdateFields();
document.removeProtectionEnforcement();
您可以使用ApachePOI提供密码保护
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("xxxxx");
OPCPackage opc = OPCPackage.open(new File("c:/test/sample.xlsx"),PackageAccess.READ_WRITE);
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
FileOutputStream fos = new FileOutputStream("c:/test/sample.xlsx");
fs.writeFilesystem(fos);
fos.close();