我試圖使用 Apache POI 將保護添加到 MS Excel 文件中,當我進行一些測試時它工作正常,但是當我在 Weblogic Server 12 運行時證明該功能時它失敗了。這是我的代碼:public static void parseFileToSecureFile(String fullFileName, String password) throws MyException{ POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes256, HashAlgorithm.sha512, -1, -1, null); Encryptor enc = info.getEncryptor(); enc.confirmPassword(password); OPCPackage opc = null; OutputStream os = null; FileOutputStream fos = null; try { opc = OPCPackage.open(new File(fullFileName), PackageAccess.READ_WRITE); os = enc.getDataStream(fs); opc.save(os); fos = new FileOutputStream(fullFileName); fs.writeFilesystem(fos); } catch (Exception e) { logger.error("error: "+e.getMessage(), e); throw new MyException("An Error"); }finally { fs.close(); fos.close(); }}public static void main(String[] args){ System.out.println("Inicia"); try { parseFileToSecureFile("/my_path/my_file.xlsx", "11"); } catch (MyException e) { e.printStackTrace(); } System.out.println("Finaliza");}正如我告訴你的,如果我從我的“主要方法”運行我的代碼“原樣”它可以工作,但如果我在運行時從 Web 應用程序調用該方法,我有下一個異常:org.apache.poi.EncryptedDocumentException: java.lang.ClassCastException: org.apache.poi.poifs.crypt.agile.AgileEncryptionInfoBuilder cannot be cast to org.apache.poi.poifs.crypt.EncryptionInfoBuilder at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:186) at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:153) at com.tets.test.test.FileCipher.parserFileToSecureFile(FileCipher.java:39) at com.test.test.test.backing.MyAdmin.doSomething(Something.java:387) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
1 回答

呼喚遠方
TA貢獻1856條經驗 獲得超11個贊
如果其他人有同樣的問題,這是我必須做的改變:
將我的 POI 版本從 v3.15 更新到 v4.0.1
添加commons-compress v1.18
此外,如果您必須閱讀加密文件,您將需要:
xmlbeans-3.0.2
公共集合4-4.1
Weblogic Server 有他自己的 xmlbeans 版本,你會遇到依賴沖突,所以,你需要在你的 weblogic.xml 文件中添加容器描述符標簽中的下一個標簽:
<prefer-web-inf-classes>false</prefer-web-inf-classes>
您還需要將下一個標簽添加到 weblogic-application.xml 文件中:
<prefer-application-packages> <package-name>org.apache.xmlbeans.*</package-name> <package-name>repackage.*</package-name> <package-name>schemaorg_apache_xmlbeans.system.*</package-name> </prefer-application-packages>
添加回答
舉報
0/150
提交
取消