[英]protect doc with password using Apache POI in java
I want to protect a .doc file with password using apache poi. 我想使用apache poi用密码保护.doc文件。 I am getting this error while running code.
运行代码时出现此错误。 please help me
请帮我
Exception in thread "main" org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format.
线程“主”中的异常org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException:提供的数据似乎采用OLE2格式。 You are calling the part of POI that deals with OOXML (Office Open XML) Documents.
您正在调用POI中涉及OOXML(Office Open XML)文档的部分。 You need to call a different part of POI to process this data (eg HSSF instead of XSSF) at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:179) at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipFile(ZipHelper.java:237) at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:134) at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:117) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:257)
您需要在org.apache.poi上的org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:179)处调用POI的不同部分来处理此数据(例如,HSSF而不是XSSF)。位于org.apache.poi.openxml4j.opc.ZipPackage。(ZipPackage.java:134)的openxml4j.opc.internal.ZipHelper.openZipFile(ZipHelper.java:237),位于org.apache.poi.openxml4j.opc.ZipPackage。( ZipPackage.java:117)位于org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:257)
POIFSFileSystem fs=new POIFSFileSystem();
EncryptionInfo info=new EncryptionInfo(EncryptionMode.agile);
Encryptor enc=info.getEncryptor();
enc.confirmPassword("user");
OPCPackage opc=OPCPackage.open("D:/Amar.doc", PackageAccess.READ_WRITE);
OutputStream os=enc.getDataStream(fs);
opc.save(os);
opc.close();
FileOutputStream stream=new FileOutputStream("D:/ao.doc");
fs.writeFilesystem(stream);
stream.close();
System.out.println("running");
I checked and reference to Apache POI documentation , it says that the password encryption for .doc file support since version 3.17, so I give it a try. 我检查并参考了Apache POI 文档 ,它说从3.17版本开始支持.doc文件的密码加密,因此我尝试一下。
It must use HWPFDocument
instead to open your doc file. 它必须改用
HWPFDocument
来打开您的doc文件。 Then you need to set password by: 然后,您需要通过以下方式设置密码:
Biff8EncryptionKey.setCurrentUserPassword(password);
Complete method: 完整的方法:
public static void encryptDocFile(File inputDocFile, File outputDocFile, String password) {
try {
FileInputStream fileInput = new FileInputStream(inputDocFile);
BufferedInputStream bufferInput = new BufferedInputStream(fileInput);
POIFSFileSystem poiFileSystem = new POIFSFileSystem(bufferInput);
// Setting password
Biff8EncryptionKey.setCurrentUserPassword(password);
HWPFDocument wordDoc = new HWPFDocument(poiFileSystem);
FileOutputStream fileOut = new FileOutputStream(outputDocFile);
wordDoc.write(fileOut);
bufferInput.close();
fileOut.close();
wordDoc.close();
System.out.println("Encrypted successfully");
} catch (IOException e) {
System.out.println("Failed to encrypt doc file");
e.printStackTrace();
}
}
Or you can checkout the complete code here : 或者,您可以在此处签出完整的代码:
If you have further question or feedback, please let me know. 如果您还有其他问题或反馈,请告诉我。 Thank you
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.