问题表现
我的poi版本是3.14
低版本的POI在 运行时,会遇到
org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to class java.util.zip.ZipFile$ZipFileInputStream
[java] Caused by: java.lang.ClassCastException: org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to java.base/java.util.zip.ZipFile$ZipFileInputStream
[java] at java.base/java.util.zip.ZipFile$ZipFileInflaterInputStream.available(ZipFile.java:478)
[java] at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.available(ZipSecureFile.java:317)
[java] at org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller.marshall(ZipPartMarshaller.java:85)
[java] at org.apache.poi.openxml4j.opc.ZipPackagePart.save(ZipPackagePart.java:124)
[java] at org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller.marshall(DefaultMarshaller.java:43)
[java] at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:585)
[java] ... 39 more
[java] 36) testAddPivotTableToWorkbookWithLoadedPivotTable(org.apache.poi.xssf.usermodel.TestXSSFWorkbook)
[java] org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to java.base/java.util.zip.ZipFile$ZipFileInputStream
[java] at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:597)
[java] at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1527)
[java] at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1510)
[java] at org.apache.poi.openxml4j.opc.ZipPackage.closeImpl(ZipPackage.java:450)
[java] at org.apache.poi.openxml4j.opc.OPCPackage.close(OPCPackage.java:470)
[java] at org.apache.poi.POIXMLDocument.close(POIXMLDocument.java:188)
[java] at org.apache.poi.xssf.usermodel.XSSFWorkbook.close(XSSFWorkbook.java:591)
[java] at org.apache.poi.xssf.usermodel.TestXSSFWorkbook.$closeResource(TestXSSFWorkbook.java:198)
[java] at org.apache.poi.xssf.usermodel.TestXSSFWorkbook.testAddPivotTableToWorkbookWithLoadedPivotTable(TestXSSFWorkbook.java:804)
...
[java] at org.apache.poi.util.OOXMLLite.build(OOXMLLite.java:149)
[java] at org.apache.poi.util.OOXMLLite.main(OOXMLLite.java:94)
[java] Caused by: java.lang.ClassCastException: org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to java.base/java.util.zip.ZipFile$ZipFileInputStream
[java] at java.base/java.util.zip.ZipFile$ZipFileInflaterInputStream.available(ZipFile.java:478)
[java] at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.available(ZipSecureFile.java:317)
[java] at org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller.marshall(ZipPartMarshaller.java:85)
[java] at org.apache.poi.openxml4j.opc.ZipPackagePart.save(ZipPackagePart.java:124)
[java] at org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller.marshall(DefaultMarshaller.java:43)
[java] at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:585)
[java] ... 39 more
问题解决:
从参考的文档 里看,可以看到,应该是jdk编译版本和运行版本不一致导致的。
我的编译版本是 jdk 8
,运行版本是jdk 17
. 符合问题表现。(从单子上看,jdk 10
以上都 有这个问题)
从单子上看,升级到 4.0.0
版本及以上可以解决。 实测升级到 4.1.2
后确实解决了。
我本地试了下小版本升级,实测升级到 3.17
版本就可以了。 不确定大版本升级的风险,最终选择了小版本升级。
参考单子:
https://bz.apache.org/bugzilla/show_bug.cgi?id=62187
https://bz.apache.org/bugzilla/show_bug.cgi?id=63270