我是 jasper 的新手,我想在 jrxml 中编译报告并从绝对路径到相对路径导出为 pdf。目前这些代码仅在绝对路径下工作。
导出为 pdf = 下载网络浏览器的文件夹
/Reports/ConsumationReport.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 应用程序的相对路径,我建议您检查这些问题。
需要找到Web应用程序路径 https://stackoverflow.com/questions/5854554/need-to-find-the-web-application-path
servletcontext.getRealPath("/") 是什么意思以及何时应该使用它 https://stackoverflow.com/questions/12160639/what-does-servletcontext-getrealpath-mean-and-when-should-i-use-it/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)