Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中实施MS Word的密码保护?_Java_Apache Poi_Docx4j - Fatal编程技术网

如何在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();