在项目中需要涉及这一块,在此之前是从未接触过的.在此记录一些方法和问题.
// 得到测试用的绑定数据
PolicyDatasource datasource = new PolicyDatasource(new PolicyService().getAll());
response.setCharacterEncoding("utf-8");
// 把模板文件读取进内存
InputStream in = this.getServletContext().getResourceAsStream(reportPath);// reportPath:模板路径
try {
// 1.在Chrome上直接显示PDF或是下载
// response.addHeader("Content-Disposition", "attachment;filename=PO.pdf");
// response.setContentType("application/pdf");
// JasperRunManager.runReportToPdfStream(in, response.getOutputStream(), new HashMap(), datasource);
// 2.
// File reportFile = new File(this.getServletContext().getRealPath(reportPath));
// byte[] bytes = null;
// try {
// bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), new HashMap(), datasource);
// response.setContentLength(bytes.length);
// out.write(bytes, 0, bytes.length);
// out.flush();
// out.close();
// } catch (JRExce