gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:27:24 26 4
gpt4 key购买 nike

首先,很抱歉,如果这个疑问如此简单。我是 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();

执行后,我收到下面打印的错误。

我关注了很多堆栈溢出问题和答案,但没有一个解决我的问题。最后一个是that .

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)
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)

我向 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>

提前致谢

最佳答案

您必须使用与现在运行时使用的不同的 apache poi 版本编译该代码。这根本行不通。

或者您的类路径中必须有来自不同 apache poi 版本的 jarThis is not supported .

正如你的堆栈跟踪所示:

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 中,OpenXML4JRuntimeExceptionZipPackage.java:552 中抛出。 .

正是 apache poi 3.15ZipPackage.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);
...

关于java - org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException - 写入 docx 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61100171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com