使用POI导出word文档时,将数据写入输出流报错

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 'app'; java.util.zip.ZipFile$ZipFileInputStream is in module java.base of loader 'bootstrap')

    at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:595)
    at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1539)
    at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:227)
    at com.xinpengtech.operation.co.order.base.service.WordUtil.exportWord(WordUtil.java:38)
    at com.xinpengtech.operation.co.order.base.service.CoOrderService.export(CoOrderService.java:778)
    at com.xinpengtech.operation.co.order.base.service.CoOrderService$$FastClassBySpringCGLIB$$977f0048.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685)
    at com.xinpengtech.operation.co.order.base.service.CoOrderService$$EnhancerBySpringCGLIB$$f95e785a.export(<generated>)
    at com.xinpengtech.operation.co.order.base.view.TranscriptController.export(TranscriptController.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 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    ......

我现在做的项目要用到导出文件的功能,然后就去学了Apache POI通过模板文件生成word文档的功能。
我写的demo在main方法和web接口下都能正常生成生成word文档;但是一移植到公司的项目上就报错了,controller和service层的业务逻辑是没问题的,调用到poi的api时才开始报错。我觉得和项目之间jar包依赖有关,因为系统刚拆了微服务,也不知道是不是这个原因,求解

/**
     * 根据模板生成word文档
     * @param inputUrl 模板路径
     * @param textMap 要替换的参数
     * @param tableList 表格参数
     */
    public static void exportWord(String inputUrl, String fileName, Map<String, String> textMap, List<String[]> tableList, HttpServletResponse response){

        try{

            // 获取docx解析对象
            XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));

            // 解析替换文本段落对象
            changeText(document, textMap);
            // 解析替换表格对象
            changeTable(document, textMap, tableList);

            // 导出到response输出流中
            ServletOutputStream os = response.getOutputStream();
            response.setHeader("Content-disposition", "attachment;fileName=" + fileName);
            document.write(os);  // 这里报错
            os.close();

        }catch(Exception e){
            e.printStackTrace();
        }
    }

1个回答

org.apache.poi.openxml4j.util.ZipSecureFile
这个类没有,缺少jar包

Mr_zhangtian
Mr_zhangtian 回复MartinQJY: 我也遇到这个问题了 请问你解决了吗
2 个月之前 回复
lightning3278
MartinQJY 有这个类呀,我引用到了...
10 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问