使用jasper生成pdf文件时报错 Error loading object from file :E:\...,请高手指点 ,急~


我用Ireport生成一个jrxml文件,ireport与jasperreport的版本都是2.0.2,在ireport界面里面是可以查看的,编译也成功了,可是当我在servlet中运行的时候却出现了错误。 

jsp文件内容如下 : 
<%@ page language="java" pageEncoding="GBK"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
    <title>Jasper test </title> 
    
<meta http-equiv="pragma" content="no-cache"> 
<meta http-equiv="cache-control" content="no-cache"> 
<meta http-equiv="expires" content="0">    
  </head> 
  <body onload="document.forms[0].submit()"> 
  <form action="jasper.pdf" method="get"> </form> 
  </body> 
</html> 

我的servlet中的方法如下: 
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
File reportFile = new File(getServletContext().getRealPath("/report/test.jrxml")); 
  Connection conn = null; 
  try { 
Class.forName("oracle.jdbc.driver.OracleDriver"); 
} catch (ClassNotFoundException e3) { 
e3.printStackTrace(); 

  String url = "jdbc:oracle:thin:@192.168.8.66:1521:idas"; 
  String username = "idastest4"; 
  String pwd = "idastest4"; 
  System.out.println("----------Connect start "); 
  
  try { 
conn = DriverManager.getConnection(url,username,pwd); 

Map reportMap = new HashMap();   
  byte[] bytes = {}; 
  
  // 产生错误的地方 
bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),reportMap,conn); 

response.setContentType("application/pdf"); 
  response.setContentLength(bytes.length); 
  
  ServletOutputStream sos = response.getOutputStream(); 
  sos.write(bytes,0,bytes.length); 
  sos.flush(); 
  sos.close(); 
  
} catch (JRException e1) { 
e1.printStackTrace(); 
} catch (SQLException e) { 
e.printStackTrace(); 
} finally { 
if(conn!=null){ 
try { 
conn.close(); 
System.out.println("----------Connect end "); 
} catch (SQLException e) { 
e.printStackTrace(); 



  

----------------------------- 
执行jsp时报错如下: 
----------Connect start 
net.sf.jasperreports.engine.JRException: Error loading object from file : E:\workspace\jasperreports\WebRoot\report\test.jrxml 
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:92) 
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:232) 
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdf(JasperRunManager.java:203) 
at com.william.jasper.controller.JasperController.service(JasperController.java:45) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151) 
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874) 
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) 
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) 
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) 
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689) 
at java.lang.Thread.run(Thread.java:595) 
Caused by: java.io.StreamCorruptedException: invalid stream header 
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:764) 
at java.io.ObjectInputStream. <init>(ObjectInputStream.java:277) 
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:87) 
... 18 more 
----------Connect end 

我注意到E:\workspace\jasperreports\WebRoot\report\test.jrxml这个文件路径是对的,且改成.jasper后缀也不行。急死了~ 
请高手帮忙解决一下,谢谢!

8 个解决方案

#1


我一般用JFillManager.fill(),这个要传.jasper文件过去,
JRunManager没用过,不知道要传的是什么文件,可能是jprint文件吧

#2


test.jrxml 只是xml文件,还没有进行编译呢,你把编译后的文件拷贝过来

一般的编译后的文件在ireport的安装目录下就能找到,名字叫做test.jasper




#3


或者你,用代码来编译这个jrxml文件,比如
       String reportDesignXML = "..........................test.jrxml";
       InputStream in = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(reportDesignXML);
       //此处将jrxml文件编译成jasper文件
       JasperReport jasperReport = JasperCompileManager.compileReport(in);
       //调用runReportToPdf来生成报表
       bytes = JasperRunManager.runReportToPdf(jasperReport,reportMap,conn); 

#4


我跟楼主出现了一样的问题,在其他地方运行好好的在servlet中运行就出现:
java.io.InvalidClassException: net.sf.jasperreports.engine.base.JRBaseReport; local class incompatible: stream classdesc serialVersionUID = 10200, local class serialVersionUID = 10003
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:85)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:62)
at com.szkingdom.report.action.ILabReportTestAction.doPost(ILabReportTestAction.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at com.szkingdom.kjax.filter.UserSessionFilter.doFilter(UserSessionFilter.java:105)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)

NESTED BY :
java.io.InvalidClassException: net.sf.jasperreports.engine.base.JRBaseReport; local class incompatible: stream classdesc serialVersionUID = 10200, local class serialVersionUID = 10003
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:85)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:62)
at com.szkingdom.report.action.ILabReportTestAction.doPost(ILabReportTestAction.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at com.szkingdom.kjax.filter.UserSessionFilter.doFilter(UserSessionFilter.java:105)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)

NESTED BY :
net.sf.jasperreports.engine.JRException: Error loading object from file : D:\mywork\webkbss\webkbss\Reports\report1.jasper
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:89)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:62)
at com.szkingdom.report.action.ILabReportTestAction.doPost(ILabReportTestAction.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at com.szkingdom.kjax.filter.UserSessionFilter.doFilter(UserSessionFilter.java:105)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.InvalidClassException: net.sf.jasperreports.engine.base.JRBaseReport; local class incompatible: stream classdesc serialVersionUID = 10200, local class serialVersionUID = 10003
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:85)
... 21 more

我用的Java代码是:
 String filePath ="D:/report1.jasper";
         List pageValue=new ArrayList();

         //生成对Jasper文件的引用
         File reportFile = new File(filePath);
         if (!reportFile.exists())
             throw new JRRuntimeException(
                     "File WebappReport.jasper not found. The report design must be compiled first.");

         //报表装填
         try{
         JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());  
         Map parameters = new HashMap();

JasperPrint jasperPrint =
                 JasperFillManager.fillReport(
                         jasperReport,
                         parameters,
                         SRDataSourceFactory.createILabReportTestDS(pageValue)
                 );
         }catch(Exception e){
         e.printStackTrace(); 
         }

#5


问题解决了:原来是jar的问题,是我的jasperreports-3.5.0.jar用到了版本过低得。

#6


O 原来如此

#7


("/report/test.jrxml")  改成 ("/report/test.jasper)

#8


这个问题还没解决吗
智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告