org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException – 写入docx时出错

huangapple go评论86阅读模式
英文:

org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException - error to write docx

问题

首先,如果这个问题太简单,我表示抱歉。我是Java的初学者。
我尝试使用Microsoft Word文档的模板来创建动态报告。
所以我有一个文件 TemplateGQ.docx,我使用 XWPFDocument doc = new XWPFDocument(OPCPackage.open("C:\\Lucas\\TemplateGQ.docx")); 来读取它,然后对其中的特定值进行一些操作,然后尝试将这个文档保存到一个新文件中。

保存代码如下:

FileOutputStream out = new FileOutputStream(new File("C:\\fusion\\Java.docx"));
doc.write(out);
out.close();

在执行后,我得到了下面打印的错误。

我查看了很多堆栈溢出的问题和答案,但是没有一个能解决我的问题。最后一个是[这个][1]。

org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : class org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to class java.util.zip.ZipFile$ZipFileInputStream (org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @314ed053; java.util.zip.ZipFile$ZipFileInputStream is in module java.base of loader 'bootstrap')
	at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:602)
	at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1557)
	at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:248)
	at com.neomind.fusion.custom.equiplex.controleMudanca.CreateReport2.start(CreateReport2.java:109)
	...

我在 pom.xml 中添加了以下依赖:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-dom</artifactId>
    <version>1.8</version>
</dependency>

提前谢谢!
[1]: https://stackoverflow.com/questions/19810214/org-apache-poi-openxml4j-exceptions-openxml4jruntimeexception-fail-to-save

英文:

First of all, sorry if this doubt is so easy. I'm a beginner in Java.
I'm trying to use a template of Microsoft Word document to create a dinamic report.
So I have a file TemplateGQ.docx, that I read using XWPFDocument doc = new XWPFDocument(OPCPackage.open(&quot;C:\\Lucas\\TemplateGQ.docx&quot;)); and after code some actions to replace specific values on it, I tryied to save this document in a new file.

To save it:

FileOutputStream out = new FileOutputStream(new File(&quot;C:\\fusion\\Java.docx&quot;));
doc.write(out);
out.close();

After execute I get the error printed below.

I followed a lot of stack overflow questions and answers, but none of them solved my problem. The last one was [that][1].

org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : class org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to class java.util.zip.ZipFile$ZipFileInputStream (org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @314ed053; java.util.zip.ZipFile$ZipFileInputStream is in module java.base of loader &#39;bootstrap&#39;)
at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:602)
at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1557)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:248)
at com.neomind.fusion.custom.equiplex.controleMudanca.CreateReport2.start(CreateReport2.java:109)
at com.neomind.fusion.workflow.adapter.CustomActivityAdapter.start(CustomActivityAdapter.java:73)
at com.neomind.fusion.workflow.handler.ActivityHandlerImpl.start(ActivityHandlerImpl.java:62)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.neomind.fusion.workflow.handler.HandlerProxy.invoke(HandlerProxy.java:33)
at com.sun.proxy.$Proxy221.start(Unknown Source)
at com.neomind.fusion.workflow.ActivityJoinXor.conditionTriggeredImpl(ActivityJoinXor.java:32)
at com.neomind.fusion.workflow.ActivityJoin.conditionTriggered(ActivityJoin.java:161)
at com.neomind.fusion.workflow.ActivitySplit.triggerCondition(ActivitySplit.java:339)
at com.neomind.fusion.workflow.ActivitySplit.split(ActivitySplit.java:180)
at com.neomind.fusion.workflow.handler.ActivityHandlerImpl.finish(ActivityHandlerImpl.java:174)
at com.neomind.fusion.workflow.handler.ActivityHandlerImpl.finish(ActivityHandlerImpl.java:37)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.neomind.fusion.workflow.handler.HandlerProxy.invoke(HandlerProxy.java:33)
at com.sun.proxy.$Proxy221.finish(Unknown Source)
at com.neomind.fusion.workflow.handler.TaskHandlerImpl.finish(TaskHandlerImpl.java:159)
at com.neomind.fusion.workflow.handler.TaskHandlerImpl.finish(TaskHandlerImpl.java:46)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.neomind.fusion.workflow.handler.HandlerProxy.invoke(HandlerProxy.java:33)
at com.sun.proxy.$Proxy222.finish(Unknown Source)
at com.neomind.fusion.workflow.service.TaskServiceImpl.complete(TaskServiceImpl.java:340)
at com.neomind.fusion.workflow.service.TaskServiceImpl.complete(TaskServiceImpl.java:332)
at com.neomind.fusion.portal.portlets.workflow.TaskPortlet.doSend(TaskPortlet.java:519)
at com.neomind.fusion.portal.portlets.workflow.TaskPortlet.processAction(TaskPortlet.java:265)
at com.neomind.fusion.portal.PortalServlet.doGet(PortalServlet.java:225)
at com.neomind.fusion.portal.PortalServlet.doPost(PortalServlet.java:102)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.neomind.fusion.security.license.filter.SecurityFilter.doFilter(SecurityFilter.java:136)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.neomind.fusion.portal.ResourcesFilter.doFilter(ResourcesFilter.java:142)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.neomind.framework.base.resources.InitializeFilter.doFilter(InitializeFilter.java:87)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.neomind.sso.filter.AuthenticationFilterChain.doFilter(AuthenticationFilterChain.java:33)
at com.neomind.sso.filter.impl.AuthenticationFilterImpl.doFilter(AuthenticationFilterImpl.java:90)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.neomind.fusion.portal.MultipartFilter.doFilter(MultipartFilter.java:88)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.neomind.fusion.portal.PortalFilter.lambda$doFilter$0(PortalFilter.java:182)
at com.neomind.fusion.persist.PersistEngine.managedRun(PersistEngine.java:3122)
at com.neomind.fusion.persist.PersistEngine.managedRun(PersistEngine.java:3050)
at com.neomind.fusion.persist.PersistEngine.managedRun(PersistEngine.java:3027)
at com.neomind.fusion.portal.PortalFilter.doFilter(PortalFilter.java:148)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.neomind.fusion.portal.SessionTimeoutFilter.doFilter(SessionTimeoutFilter.java:83)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)

I added to the pom.xml this dependencies:

&lt;dependency&gt;
    &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
    &lt;artifactId&gt;poi-ooxml&lt;/artifactId&gt;
    &lt;version&gt;4.1.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.apache.xmlgraphics&lt;/groupId&gt;
    &lt;artifactId&gt;batik-dom&lt;/artifactId&gt;
    &lt;version&gt;1.8&lt;/version&gt;
&lt;/dependency&gt;

Thanks in advance
[1]: https://stackoverflow.com/questions/19810214/org-apache-poi-openxml4j-exceptions-openxml4jruntimeexception-fail-to-save

答案1

得分: 4

你肯定是用了不同版本的 apache poi 编译了那段代码,而现在运行时使用的版本是不同的。这样根本行不通。

或者你的类路径中可能存在来自不同 apache poi 版本的 jar 包。这是不支持的

正如你的堆栈跟踪所示:

org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: ...
at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:602)

异常是从 ZipPackage.java 的代码行 602 抛出的。

但在版本 4.1.2 中,ZipPackage.java 甚至没有代码行 602。在 apache poi 4.1.2 中,OpenXML4JRuntimeException 是在 ZipPackage.java:552 处抛出的。

而在 apache poi 3.15 中,该异常是在 ZipPackage.java:602 处抛出的。

因此,在运行代码时,你的类路径中必定存在一个 poi-ooxml-3.15.jar

你可以通过将以下代码行放在在异常抛出之前运行的代码中来测试 ZipPackage 的来源:

...
ClassLoader classloader = org.apache.poi.openxml4j.opc.ZipPackage.class.getClassLoader();
java.net.URL res = classloader.getResource("org/apache/poi/openxml4j/opc/ZipPackage.class");
String path = res.getPath();
System.out.println("ZipPackage came from " + path);
...
英文:

You must have compiled that code using a different apache poi version as now is used while running. That cannot work at all.

Or you must have jars from different apache poi versions in your class path. This is not supported.

As your stacktrace tells:

org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: ...
at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:602)

The exception is thrown from ZipPackage.java, code line 602.

But in version 4.1.2 there is not even a code line 602 in ZipPackage.java. In apache poi 4.1.2 the OpenXML4JRuntimeException is thrown in ZipPackage.java:552.

It is apache poi 3.15 where that exception is thrown in ZipPackage.java:602.

So you must have a poi-ooxml-3.15.jar somewhere in class path while running the code.

You can test where your ZipPackage came from while running, if you put the following code lines somewhere in code which runs before the exception is thrown:

...
ClassLoader classloader = org.apache.poi.openxml4j.opc.ZipPackage.class.getClassLoader();
java.net.URL res = classloader.getResource(&quot;org/apache/poi/openxml4j/opc/ZipPackage.class&quot;);
String path = res.getPath();
System.out.println(&quot;ZipPackage came from &quot; + path);
...

huangapple
  • 本文由 发表于 2020年4月8日 19:59:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定