Jasper Reports改变相对路径的绝对路径?

时间:2015-10-26 08:57:44

标签: java jasper-reports absolute relative

我是jasper的新手,我想在jrxml中编译报告并从绝对路径导出到pdf in到相对路径。目前,这些代码仅适用于绝对路径。

导出到pdf =下载Web浏览器的文件夹 /Reports/ConsumptionReport.jrxml中的jrxml(在网页内) 谢谢你:D

public void showReport(int productionNumber) throws JRException {

        try {




            DBConnectionFactory myFactory = DBConnectionFactory.getInstance();
            Connection conn = myFactory.getConnection();

            Map map = new HashMap();
            map.put("prodNum", productionNumber);

            JasperReport jr = JasperCompileManager.compileReport("/Users/user/NetBeansProjects/EGMI/web/Reports/ConsumptionReport.jrxml");
            //Fill the report with parameter, connection and the stream reader     
            JasperPrint jp = JasperFillManager.fillReport(jr, map, conn);

            JasperExportManager.exportReportToPdfFile(jp, "/Users/user/NetBeansProjects/EGMI/web/Reports/ConsumptionReport.pdf");
            JasperViewer.viewReport(jp, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

文件夹层次结构

EGMI
   ---Web Pages
        ----Reports
               -----ConsumptionReport.jrxml 

SOLUTION-servlet

String relativeWebPath = "/Reports";
           String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
           File f = new File(absoluteDiskPath, "ConsumptionReport.jrxml");

1 个答案:

答案 0 :(得分:1)

使用java.io.File从相对路径获取绝对路径。 ES。

File f = new File("yourRelativePath/ConsumptionReport.jrxml");
JasperReport jr = JasperCompileManager.compileReport(f.getAbsolutePath());  

查看问题以找到已部署的Web应用程序的相对路径我建议您查看这些问题。

Need to find the web application path

What does servletcontext.getRealPath("/") mean and when should I use it