java - 使用 Apache poi 将数据写入 Word 文档?

标签 java apache-poi aes fileoutputstream xwpf

我正在实现 AES 算法 128 位 key 。加密后,加密数据的前 16 字节将存储在 .docx 文件中。之后.docx 文件将被阻止。

XWPFDocument document = new XWPFDocument() ;
FileOutputStream out = new FileOutputStream(filename,true);//filename is .docx word document
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(ress1);//ress1 is a String datatype
document.write(out);

最佳答案

根据我从您的评论中了解到的,您想要加密您的Word文件。您可以使用以下代码片段来实现:

POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);

Encryptor enc = info.getEncryptor();
enc.confirmPassword(<your_password>);


OPCPackage opc = OPCPackage.open(new File(<file_path>), PackageAccess.READ_WRITE); //opening package for encryption 
OutputStream os = enc.getDataStream(fs); //perform encryption 
opc.save(os); //save package
opc.close();

FileOutputStream fos = new FileOutputStream("file_path"); 
fs.writeFilesystem(fos); //write the file back to file system
fos.close();

关于java - 使用 Apache poi 将数据写入 Word 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38105235/

相关文章:

java - 通过 Apache POI 将 java 程序的输出存储在 Excel 工作表中

c++ - 将亚洲和其他字符放入 Visual Studio 源代码中

java - DeepLearning4J:在 jupyter 中运行时出现 org.nd4j.linalg.factory.Nd4jBackend$NoAvailableBackendException

Java+Kotlin Jar 只能与 Gradle 应用程序插件一起运行

java - 如何使用 Apache POI 将网格线添加到 Excel 折线图

java - 如何加快 apache POI 中列的自动调整大小?

python - AES 加密 Golang 和 Python

encryption - 当加密不是 block 大小的偶数倍的数据时,我必须发送完整的最后一个 block 吗?

java - 由 JPanel 实现时,MouseEvent 不会触发

java - 创建扫雷,更改难度时如何重置棋盘。