jasper Could not load object from location

依然范特西╮ 提交于 2019-12-25 07:49:46

问题


I want to get a JasperReport from a jasper file path. My jasper file path is C:/project/report/reportTemplate_20110420000035982.jasper

public static JasperReport getJasperReportByTemplateName(String templateFullName) throws JRException{
    JasperReport jasperReport;
    jasperReport = (JasperReport)JRLoader.loadObjectFromLocation(templateFullName);
    return jasperReport;
}

and an exception occurs at this line:

jasperReport = (JasperReport)JRLoader.loadObjectFromLocation(templateFullName);

I tried to find the reason, but it looks like everything is ok. I guess may be the path is not valid in Windows (C:/project/report/reportTemplate_20110420000035982.jasper)

Anyone who can help me, thanks a lot. I've pasted the exception info below. Thanks.


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)

回答1:


Looking at the source code of JRLoader at http://jasperreports.sourcearchive.com/documentation/3.6.0plus-pdfsg/JRLoader_8java-source.html, it looks like this line is returning null for your file.

File file = JRResourcesUtil.resolveFile(location, fileResolver);

which seems to suggest the file is either missing or not a file (i.e. a directory).

If you call:

new File("C:/project/report/reportTemplate_20110420000035982.jasper").exists()

do you get true or false?

Could you try calling JRLoader#loadObject() passing in a FileInputStream that you create yourself?




回答2:


I use the technique like this to make sure of the path

  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();
            }
        }
    }


来源:https://stackoverflow.com/questions/5725832/jasper-could-not-load-object-from-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!