gpt4 book ai didi

java - 打印报告使 Apache 无响应

转载 作者:行者123 更新时间:2023-12-01 14:22:13 25 4
gpt4 key购买 nike

当我通过jsp打印报告时出现问题。

首先,我认为它更像是内存错误,即 NetBeans 的永久代空间,但当您部署应用程序并且它变为 Activity 状态时,它不应该发生,但问题仍然存在。

对于前 6-7 个报告,报告打印工作正常,然后应用程序变得无响应,为了使其再次工作,我必须停止 Apache 并再次运行应用程序

这是我用来通过 jsp 打印报告的代码

try {
JasperReport jasperReport;
JasperPrint jasperPrint;
JasperDesign jasperdesign;
String path = getServletContext().getRealPath("/reports/admissionReport.jrxml");
String imagepath = getServletContext().getRealPath("/images//");
String imageSubPath = File.separatorChar + "";
imagepath = imagepath + imageSubPath;
jasperdesign = JRXmlLoader.load(path);
jasperReport = JasperCompileManager.compileReport(jasperdesign);
Connection con = this.getServiceFactory().getReportService().getDao().getJdbcTemplate().getDataSource().getConnection();
paramMap.put("imagePath", imagepath);
jasperPrint = JasperFillManager.fillReport(jasperReport, paramMap, con);
JasperExportManager.exportReportToPdfFile(jasperPrint, getServletContext().getRealPath("/reports/admissionReport.pdf"));
String filename = getServletContext().getRealPath("/reports/admissionReport.pdf");
File rtf = new File(filename);
int readBytes = 0;
response.setContentType("application/pdf");
response.setHeader("Cache-Control", "max-age=30");
response.setHeader("Content-disposition", "inline; filename=\"admissionReport\"");
response.setContentLength((int) rtf.length());
input = new FileInputStream(rtf);
BufferedInputStream buf = new BufferedInputStream(input);
OutputStream stream = response.getOutputStream();
while ((readBytes = buf.read()) != -1) {
stream.write(readBytes);
}
stream.flush();
stream.close();
} catch (Exception exp) {
throw new Exception("Error occured while saving record.." + exp.getMessage());
} finally {
if (input != null) {
input.close();
}
}

到目前为止我无法通过互联网找到解决方案。

P.S 我正在使用Spring框架进行开发。

我想知道是否有人能解决这个问题。

最佳答案

buf.close() 缺失;则不需要 input.close() 。目前有点不确定 stream.close() 是否是禁忌 - 但它有效。您可以尝试con.close()

提示:

response.setHeader("Content-Length", String.valueOf(rtf.length()));

Files.copy(rtf.toPath(), stream);

最后一个是 Java 7 实用程序,用于将文件复制到流,取代按字节读取,即使使用 BufferedInputStream,这也不是最佳选择。

关于java - 打印报告使 Apache 无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17443430/

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