有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java打印报告使Apache无响应

当我通过jsp打印报告时,问题就出现了

首先,我认为这更像是一个内存错误,即NetBeans的permgen空间,但在部署应用程序并使其变为活动状态时,它不应该发生,但问题仍然存在

报告打印可以很好地用于前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();
    }
}

到目前为止,我无法在互联网上找到解决方案

另外,我正在使用Spring框架进行开发

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


共 (1) 个答案

  1. # 1 楼答案

    buf.close()缺失input.close()那就不需要了。目前有点不确定stream.close()是否是禁忌症——但它确实有效。你可以试试

    提示:

    response.setHeader("Content-Length", String.valueOf(rtf.length()));
    
    Files.copy(rtf.toPath(), stream);
    

    最后一个是Java 7实用程序,用于将文件复制到流中,取代字节读取,即使使用BufferedInputStream也不是最优的