有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JasperReports报告没有在Spring MVC应用程序中出现

我将JasperReports集成到我的springMVC网站中。 它在我的本地系统中运行良好,但当我将该网站上传到服务器时,报告正在生成,但它并没有像在我的本地系统中那样弹出

我正在使用iReport4.1

在上传网站之前,我还更改了报告的路径。 报告在目标文件夹生成,但不会自动显示

这是我的代码:

jasperReport = JasperCompileManager.compileReport("C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\CallCenterRev\\reports\\AttendanceReport.jrxml");
//JasperFillManager.fillReportToFile("D:\\reports\\test.jasper", jasperParameter, rsss);
jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameter, rsss);
//JasperPrintManager.printReport(jasperPrint,true);

JasperExportManager.exportReportToPdfFile(jasperPrint, "C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\CallCenterRev\\reports\\AttendanceReport.pdf");
//        new mainpage(getTitle());

if ((new File("C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\CallCenterRev\\reports\\AttendanceReport.pdf")).exists()) {
    Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\CallCenterRev\\reports\\AttendanceReport.pdf");
    p.waitFor();

共 (2) 个答案

  1. # 1 楼答案

    如果您使用Spring3,这可能会有所帮助

        @Controller
        @RequestMapping(value="/report")
        public class ReportsController
        {
            @RequestMapping(value="/getMyReport", method=RequestMethod.GET)
            public void runReport(@RequestParam("someParam")String someParam,@RequestParam("someOtherParam")String someOtherParam,HttpServletRequest request,HttpServletResponse response)
            {
                InputStream is = null ;
                is = request.getSession().getServletContext().getResourceAsStream("/WEB-INF/reports/myReport.jasper");
                Map paramMap = new HashMap();
                paramMap.put("someParam", someParam);
                paramMap.put("someOtherParam", someOtherParam);
                response.setContentType("application/pdf");
                response.setHeader("Content-Disposition", "inline; filename=myReport.pdf");
                try {
                    Connection connection =getDatabaseConnection();//let this method returns a database connection
                    JasperRunManager.runReportToPdfStream(is, response.getOutputStream(), paramMap, connection);
                    response.getOutputStream().flush();
                    response.getOutputStream().close();
                }
                catch (Exception e)
                {
                   //may be some Exception handling
    
                }
            }
        }
    
  2. # 2 楼答案

    首先,为什么要使用绝对路径。我认为应该使用相对路径(ServletContext.getRealPath())。 第二,这个代码是什么

    Process p = Runtime
           .getRuntime()
           .exec("rundll32 url.dll,FileProtocolHandler C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\CallCenterRev\\reports\\AttendanceReport.pdf");
        p.waitFor();
    

    当然,它不会显示在web浏览器中。要在浏览器中查看报告,请将pdf写入http servletresponse并相应地设置http标头