【问题标题】:java.io.StreamCorruptedException: invalid stream header: EFBFBDEFjava.io.StreamCorruptedException:无效的流标头:EFBFBDEF
【发布时间】:2014-06-06 09:44:46
【问题描述】:

我正在开发一个 java web 应用程序,我正在使用类似 maven 的项目管理工具,现在我对这个问题变得疯狂。 我尝试从我的应用程序构建一个碧玉报告,该报告由我在 src/main/resources 中的许多子报告组成。 我尝试通过以下方式加载这些子报表:

//Sub Report 1 
InputStream fileSubRep1=(BufferedInputStream) getClass().getResourceAsStream("/fileSubReport1.jasper");
map.put("fileSubRep1",(JasperReport) JRLoader.loadObject(fileSubRep1));

//Sub Report 2 
InputStream fileSubRep2=(BufferedInputStream) getClass().getResourceAsStream("/fileSubReport2.jasper");
map.put("fileSubRep1",(JasperReport) JRLoader.loadObject(fileSubRep2));

问题是调用这个方法JRLoader.loadObject(fileSubRep1)会产生如下异常:

        net.sf.jasperreports.engine.JRException: Error loading object from InputStream
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:248)
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:228)
    at it.aiemonline.report.jasper.GeneraFattureServiceImpl.createMapFileJasper(GeneraFattureServiceImpl.java:292)
    at it.aiemonline.report.jasper.GeneraFattureServiceImpl.executeStreamByteFatturaPdf(GeneraFattureServiceImpl.java:890)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
    at it.aiemonline.utility.logger.SystemLogger.aroundExecuteLog(SystemLogger.java:187)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy78.executeStreamByteFatturaPdf(Unknown Source)
    at it.aiemonline.vaadin.application.gui.fatturazione.visualizza.VisualizzaFatturaPanel.visualizzaPdf(VisualizzaFatturaPanel.java:236)
    at it.aiemonline.vaadin.application.gui.fatturazione.visualizza.VisualizzaFatturaPanel$1.itemClick(VisualizzaFatturaPanel.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:969)
    at com.vaadin.ui.Table.handleClickEvent(Table.java:3057)
    at com.vaadin.ui.Table.changeVariables(Table.java:2853)
    at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:396)
    at com.vaadin.server.communication.ServerRpcHandler.handleBurst(ServerRpcHandler.java:221)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:111)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:91)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1382)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:538)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:478)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:517)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:225)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:937)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:406)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:871)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:247)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
    at org.eclipse.jetty.server.Server.handle(Server.java:346)
    at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:589)
    at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1065)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:823)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:220)
    at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:411)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:535)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:529)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:58)
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:243)
    ... 69 more

我哪里做错了? 我该如何解决?

【问题讨论】:

    标签: java maven jasper-reports subreport


    【解决方案1】:

    在头痛了 3 天之后,我终于解决了我的问题。我正在使用类似 maven 的项目管理工具,并且正在开发具有这种结构的模块化项目

    |-- parent
       |-- model
        --pom.xml
       |-- services
        --pom.xml
       |-- web-app
        --pom.xml
    

    问题是我尝试像输入流一样加载的文件位于服务模块的 src/main/resources 中,但在 web-app 的 pom.xml 中我启用了资源过滤,并且由于该 web- app 模块依赖于服务,过滤是在服务模块中扩展的。

    在maven网站Filtering的过滤部分我发现:

    警告:不要过滤带有二进制内容的文件,例如图片!这很可能会导致输出损坏。如果你有文本文件和二进制文件作为资源,你需要声明两个互斥的资源集。第一个资源集定义要过滤的文件,另一个资源集定义要复制的文件,如下所示:

    然后我从我的 web-app 模块中删除了以下代码,一切正常

    <resources>
         <resource>
             <directory>src/main/resources</directory>
             <filtering>true</filtering>
        </resource>
    </resources>
    

    【讨论】:

    • 兄弟,你节省了我两天的时间,我使用 UTF-8 字体的 jasper 报告,但使用微服务 spring 打印失败:D
    【解决方案2】:

    感谢@skizzo 提供此解决方案

    如果您仍然需要过滤一些其他文件,或者复制所有文件,那么一种不太激进的方法就是简单地包含您需要过滤的所有文件(java、xml、属性...)。

    感谢您的帮助,这是我刚刚实施的解决方案:

            <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.java</include>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>**/*.java</exclude>
                    <exclude>**/*.xml</exclude>
                    <exclude>**/*.properties</exclude>
                </excludes>
            </resource>
        </resources>
    

    它似乎根本不相关,我已经找了几个小时了。它真的拯救了我的一天! ;-)

    【讨论】:

      【解决方案3】:

      我遇到了完全相同的问题,在我的 pom.xml 中添加以下插件解决了

              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-resources-plugin</artifactId>
                  <version>3.0.2</version>
                  <configuration>
                    <nonFilteredFileExtensions>
                      <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
                      <nonFilteredFileExtension>jasper</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                  </configuration>
                </plugin>
      

      【讨论】:

        【解决方案4】:

        就我而言,我忘记将已编译的 jasper 报告从行尾固定 ant 任务中排除。对我来说,以下排除解决了这个问题:

        <fixcrlf srcdir="${basedir}/target/etc"
            includes="**/*.*"
            excludes="**/*.jasper"
            eol="lf" eof="remove"/>
        

        【讨论】:

          【解决方案5】:

          对我来说,原因不是糟糕的 Maven 配置。 我只是在生成报告时提供了一个错误名称的参数。

          【讨论】:

            【解决方案6】:

            就我而言,问题是我的 Web 应用程序上的 JasperReport 版本与 .jasper 文件不同。

            很可能是用较新的版本编译的(我认为是外部的 Tibco 设计)。

            【讨论】:

              猜你喜欢
              • 2011-02-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多