与互联网下载管理器打印问题

时间:2017-02-04 07:37:05

标签: java spring servlets jasper-reports

我想使用servlet在我的java Web应用程序中打印文档,并清除报告信息以避免下载恢复(SecurityContext.cleanReportInfo())。 当你的系统中没有IDM时,一切都很好但是当IDM正在运行时,它会发送2个请求而不是1,并且通过重定向到/ 403就会出现问题,如下面的代码所示。

PrintReportServlet

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ReportDto reportInfo = SecurityContext.getReportInfo();
        if (reportInfo != null) {
            ServletOutputStream outputStream = response.getOutputStream();
            try {
                generateHeaders(response, reportInfo);
                generateContents(outputStream, reportInfo);
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, "Error occurred during print report, nested error message: {0}", e.getMessage());
            } finally {
                outputStream.flush();
                outputStream.close();
                SecurityContext.cleanReportInfo();
            }
        } else {
            response.sendRedirect(request.getContextPath() + "/403");
        }
    }

SecurityContext.java

public static void cleanReportInfo() {
    SecurityContext.getCurrentSession().setAttribute("Report_Info", null);
}

errorreportController.js

$scope.printErrorList = function () {
    errorReportService.printErrorList(function () {

        window.open(contextPath + "/PrintReport");
    });
};

ErrorReportController.java

@RequestMapping(value = "/printError", method = RequestMethod.GET)
@ResponseBody
public String printError() {
    ReportDto reportDto = errorReportService.getReportInfo();
    SecurityContext.setReportInfo(reportDto);
    return success();
}

ErrorReportServiceImpl

public ReportDto getReportInfo() {
    Map<String, Object> parameters = new HashMap<String, Object>();

    parameters.put("jalaliDate", DateUtil.convertToJalali(new Date()).toStringBySlash());
    parameters.put("alahImagePath", ReportUtil.getImagesPath() + File.separator + "alah.png");
    parameters.put("sepahImagePath", ReportUtil.getImagesPath() + File.separator + "sepah.png");
    parameters.put("reporter", SecurityContext.getCurrentUserName());
    parameters.put("classify", "Normal");
    parameters.put("userId",SecurityContext.getCurrentUserId());

    ReportDto reportDto = new ReportDto(
            ReportNames.ErrorReport, DataItems.Report_Type_PDF,
            "error",
            parameters);
    return reportDto;
}

0 个答案:

没有答案