Jasper Reports将绝对路径更改为相对路径? [英] Jasper Reports Change absolute path to relative path?

查看:290
本文介绍了Jasper Reports将绝对路径更改为相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jasper的新手,我想在jrxml中编译报告并从绝对路径到相对路径导出到pdf.目前,这些代码仅在绝对路径下有效.

I am new to jasper, I want to compile the report in jrxml and export to pdf in from absolute path to relative path. Currently the codes work only in absolute path.

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

export to pdf = download folders of web browser jrxml inside the /Reports/ConsumptionReport.jrxml (inside the web pages) thank you :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 

解决方案-servlet

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

推荐答案

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

Use java.io.File to get the absolute path from a relative path. es.

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

看到问题以找到已部署的Web应用程序的相对路径,建议您检查一下这些问题.

Seeing the problem to find the relative path of your deployed web application I suggest you check out these questions.

需要找到Web应用程序路径

servletcontext.getRealPath是什么( "//")的含义以及何时应使用

这篇关于Jasper Reports将绝对路径更改为相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆