【问题标题】:Jasper Reports Change absolute path to relative path?Jasper Reports 将绝对路径更改为相对路径?
【发布时间】:2015-10-26 08:57:43
【问题描述】:

我是 jasper 的新手,我想在 jrxml 中编译报告并从绝对路径导出到 pdf 到相对路径。目前代码只能在绝对路径下工作。

导出为 pdf = 下载网络浏览器的文件夹 /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 

解决方案-servlet

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

【问题讨论】:

标签: java jasper-reports relative-path


【解决方案1】:

使用 java.io.File 从相对路径中获取绝对路径。是的。

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

【讨论】:

  • 我把它改成了 File f = new File("Reports/ConsumptionReport.jrxml"); JasperReport jr = JasperCompileManager.compileReport(f.getAbsolutePath());但现在的问题是它走错了路
  • 您需要知道从哪个相对路径开始...尝试 System.out.println(f.getAbsolutePath()) 以了解您在哪里...以及您指向的内容。
  • ava.io.FileNotFoundException: /Applications/NetBeans/glassfish-4.1/glassfish/domains/domain1/config/Reports/ConsumptionReport.jrxml 这是错误,有没有办法定位相对路径第一?
  • 你的相对路径的开始是:/Applications/NetBeans/glassfish-4.1/glassfish/domains/domain1/config/ 它是在应用程序启动时设置的...
  • File f = new File("/Applications/NetBeans/glassfish-4.1/glassfish/domains/domain1/config/EGMI/web/Reports/ConsumptionReport.jrxml"); 我试过这个,但它仍然不起作用,我将在问题中更新我的文件夹层次结构。谢谢你:))
猜你喜欢
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 2011-11-07
  • 2011-05-02
相关资源
最近更新 更多