JasperReports:使用Spring MVC导出报告

时间:2013-09-25 14:51:41

标签: java spring jasper-reports

我正在尝试使用Zip格式的 Spring MVC AngularJS 从我的本地计算机下载 JasperReports 报告。我从JSF代码中获取了cue,并试图在 Spring 中复制它。但FacesContext.getCurrentInstance()正在返回null,这就是代码爆炸的地方。我想要一些帮助,也许可以替代这个FacesContext或其他一些我非常感谢的建议。

我的服务处理程序:

public SubmissionReportDto excelDownload(Map<String, String> contractData) throws IOException {
    String contractId = contractData.get(DBConstants.PROC_PI_CONTRACT_ID);
    String versionNo = contractData.get(DBConstants.VERSION_NO);

    ZipOutputStream zos = null;
    String zipFileName = "";
    String fileName = "";

    submissionReportJasperDao = new SubmissionReportJasperDaoImpl();

    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

    try {
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
        submissionReportDto.setReportName("Billing Related Parameters.jasper");
        String reportName = submissionReportDto.getReportName();

        byte[] buffer = submissionReportJasperDao.mainReportXLS(contractId, versionNo, reportName);
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=" + zipFileName);
        response.setHeader("Pragma", "public");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Cache-Control", "public");
        response.setHeader("Content-Description", "File Transfer");
        response.setContentType("application/xls");
        response.setContentType("application/zip");

        try {
            zos = new ZipOutputStream(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setLevel(Deflater.BEST_COMPRESSION);
        ZipEntry entry = new ZipEntry(fileName);
        try {
            zos.putNextEntry(entry);
            zos.write(buffer);
            zos.write(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (Exception e) {
        System.out.print("Exception" + e);

    } finally {
        if (null != zos) {
            zos.flush();
            zos.closeEntry();
            zos.close();
        }
    }

    facesContext.responseComplete();
    submissionReportDto.setSuccessMessage("Download Success");
    return submissionReportDto;
}

0 个答案:

没有答案