压缩多个jasper报告并通过电子邮件发送此zip

时间:2015-09-25 14:35:40

标签: java email jasper-reports

我目前通过电子邮件发送Jasper Report(发票)和javax邮件。它工作正常:

InputStream reportStream;
reportStream = this.getClass().getResourceAsStream(TEMPLATE_INVOICE); 
JasperDesign jd;

jd = JRXmlLoader.load(reportStream);
JasperReport jr = JasperCompileManager.compileReport(jd);
for (Long invoiceId:invoicesId){
    List<Object[]> results = invoiceRepository.findAllByIdInvoice(invoiceId);
    JasperPrint jp = JasperFillManager.fillReport(jr, new HashMap<String,Object>(), dataProvider.getInvoice(results));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JasperExportManager.exportReportToPdfStream(jp, baos);
    mailService.sendMail(baos);
}

现在我想通过电子邮件发送一张包含我所有发票的邮箱 在每次迭代中,我都会将生成的jasper报告添加到zip文件中,然后我会通过电子邮件发送此zip文件 有没有办法做到这一点?

由于

1 个答案:

答案 0 :(得分:1)

这个问题与碧玉报告无关......

您可以使用此功能将一个或多个文件添加到zip文件中(您需要根据需要进行编辑)。

实际版本作为输入参数获取二维数组(存储filePath和fileName)...

public static byte[] createZipFile(String [][] files) {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ZipOutputStream zipfile = new ZipOutputStream(bos);
        byte[] buf = new byte[1024];        
        String fileName = null;     
        int i = 0;
        try {
            while (i < files.length) {

                String [] singleFile = files[i];                    
                fileName = singleFile[0];
                File f = new File(fileName);                
                if (f.exists())
                {
                    FileInputStream in = new FileInputStream(fileName);                                
                    zipfile.putNextEntry(new ZipEntry(i + "_" + singleFile[1]));
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        zipfile.write(buf, 0, len);
                    }
                    in.close();
                }
                i++;
            }
            zipfile.close();
        } catch (IOException e) { }
        return bos.toByteArray();
    }