我目前正在通过 javax 邮件通过电子邮件发送 Jasper 报告(发票)。它工作正常:
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);
}
现在我想通过电子邮件发送包含我所有发票的 zip 文件。
在每次迭代中,我都会将生成的 jasper 报告添加到 zip 文件中,然后通过电子邮件发送此 zip 文件。
有没有办法做到这一点?
谢谢
最佳答案
这个问题与贾斯珀报告无关......
您可以使用此功能将一个或多个文件添加到 zip 文件中(您需要根据需要进行编辑)。
实际版本获取二维数组作为输入参数(存储文件路径和文件名)...
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();
}
关于java - 压缩多个 jasper 报告并通过电子邮件发送此 zip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32784604/