我想从jasper文件路径获取JasperReport。我的jasper文件路径是C:/project/report/reportTemplate_20110420000035982.jasper
public static JasperReport getJasperReportByTemplateName(String templateFullName) throws JRException{
JasperReport jasperReport;
jasperReport = (JasperReport)JRLoader.loadObjectFromLocation(templateFullName);
return jasperReport;
}
并且此行发生异常:
jasperReport = (JasperReport)JRLoader.loadObjectFromLocation(templateFullName);
我试图找到原因,但看起来一切正常。我想可能是路径不是
在Windows中有效(C:/project/report/reportTemplate_20110420000035982.jasper
)
任何可以帮助我的人,非常感谢。我已粘贴下面的异常信息。感谢。
net.sf.jasperreports.engine.JRException: Could not load object from location : C:/project/report/reportTemplate_20110420000035982.jasper
at net.sf.jasperreports.engine.util.JRLoader.loadObjectFromLocation(JRLoader.java:262)
at net.sf.jasperreports.engine.util.JRLoader.loadObjectFromLocation(JRLoader.java:221)
at com.hp.proctool.reportmgt.customizer.IReportFactory.getJasperReportByTemplateName(IReportFactory.java:68)
at com.hp.proctool.reportmgt.service.ReportManager.getParametersByTemplateName(ReportManager.java:410)
at com.hp.proctool.reportmgt.service.ReportManager.getFieldJs(ReportManager.java:360)
at com.hp.proctool.reportmgt.service.ReportManager.generateReportDownloadForm(ReportManager.java:345)
at com.hp.proctool.reportmgt.service.ReportManager$$FastClassByCGLIB$$c444a9f1.invoke()
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
at com.hp.proctool.reportmgt.service.ReportManager$$EnhancerByCGLIB$$1ab1a55e.generateReportDownloadForm()
at com.hp.proctool.reportmgt.web.ReportController.generateDownloadForm(ReportController.java:148)
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.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:421)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:136)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.hp.common.orm.RightFilter.doFilter(RightFilter.java:73)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:2)
在http://jasperreports.sourcearchive.com/documentation/3.6.0plus-pdfsg/JRLoader_8java-source.html查看JRLoader
的源代码,看起来此行正在为您的文件返回null
。
File file = JRResourcesUtil.resolveFile(location, fileResolver);
似乎暗示文件丢失或不是文件(即目录)。
如果你打电话:
new File("C:/project/report/reportTemplate_20110420000035982.jasper").exists()
你是真的还是假的?
您可以尝试拨打JRLoader#loadObject()
传递自己创建的FileInputStream
吗?
答案 1 :(得分:0)
我使用这样的技术来确保路径
String subReportDir = ((HashMap<String, String>) dispatcher.getServletContext().getAttribute("local")).get("SUBREPORT_DIR");
String reportPath = dispatcher.getServletContext().getRealPath(subReportDir) + "\\";
String reportNamePath = dispatcher.getServletContext().getRealPath(subReportDir + reportName);
HashMap map = new HashMap();
map.put("SUBREPORT_DIR", reportPath);//this is a parameter in the report in the subreport only put the report name.jasper
String dbConnection = (String) dispatcher.getServletContext().getInitParameter("dbConnectionString");
String dbUserName = (String) dispatcher.getServletContext().getInitParameter("dbUserName");
String dbPassword = (String) dispatcher.getServletContext().getInitParameter("dbPassword");
ReportGenerator reportGenerator;
reportGenerator = new ReportGenerator(reportNamePath, map, dbConnection, dbUserName, dbPassword);//
byte[] reportSteam = null;
SimpleDateFormat dt = new SimpleDateFormat("dd/MM/yyyy");
String fileName = "Issue Statistics" + dt.format(new Date());
reportSteam = reportGenerator.generateRTFReport();
dispatcher.getRequest().setAttribute("mimeType", "application/msword");
dispatcher.getRequest().setAttribute("fileName", fileName + ".doc");
dispatcher.getRequest().setAttribute("fileBytes", reportSteam);
public ReportGenerator(String reportPath, HashMap map, String dbConnection, String dbUserName, String dbPassword) {
this.reportPath = reportPath;
this.map = map;
this.dbConnection = dbConnection;
this.dbUserName = dbUserName;
this.dbPassword = dbPassword;
}
public byte[] generateRTFReport()
{
try
{
jasperReport = (JasperReport) JRLoader.loadObject(reportPath);
jasperPrint = JasperFillManager.fillReport(jasperReport, map, connectDB(dbConnection, dbUserName, dbPassword));
rtfExporter = new JRRtfExporter();
reportSteam = new ByteArrayOutputStream();
rtfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, reportSteam);
rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
rtfExporter.exportReport();
System.out.println("jasper loaded");
return reportSteam.toByteArray();
}
catch(Exception ex)
{
ex.printStackTrace();
return null;
}
finally
{
try
{
reportSteam.close();
}
catch(IOException io)
{
io.printStackTrace();
}
}
}