简体   繁体   English

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

[英]Jasper Reports Change absolute path to relative path?

I am new to jasper, I want to compile the report in jrxml and export to pdf in from absolute path to relative path. 我是jasper的新手,我想在jrxml中编译报告并从绝对路径导出到pdf in到相对路径。 Currently the codes work only in absolute path. 目前,代码仅在绝对路径中起作用。

export to pdf = download folders of web browser jrxml inside the /Reports/ConsumptionReport.jrxml (inside the web pages) thank you :D 导出到pdf =下载/Reports/ConsumptionReport.jrxml中的Web浏览器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();
        }
    }

folder hierarchy 文件夹层次

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

SOLUTION- servlet 解决方案 - servlet

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

Use java.io.File to get the absolute path from a relative path. 使用java.io.File从相对路径获取绝对路径。 es. ES。

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

Seeing the problem to find the relative path of your deployed web application I suggest you check out these questions. 查看问题以查找已部署的Web应用程序的相对路径,我建议您查看这些问题。

Need to find the web application path 需要找到Web应用程序路径

What does servletcontext.getRealPath("/") mean and when should I use it servletcontext.getRealPath(“/”)是什么意思,什么时候应该使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM