gpt4 book ai didi

java - Jasper Reports 将绝对路径更改为相对路径?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:08 24 4
gpt4 key购买 nike

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

导出为 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

解决方案- servlet

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

最佳答案

使用 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

关于java - Jasper Reports 将绝对路径更改为相对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33341822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com