【发布时间】:2020-07-27 18:38:45
【问题描述】:
我想压缩在数据表中选择的多个 pdf 文件并让用户下载它们。
这里是 XHTML;
<p:commandLink id="print_orders"
value="Print Selected Orders" ajax="false"
onclick="PrimeFaces.monitorDownload(startPrint, stopPrint);"
styleClass="button button--ujarak button--border-thin button--text-medium download"
style="text-align: center; float:none; margin: 0px auto 0px auto; padding: 0.05em 0.1em;" >
<p:fileDownload value="#{printOrdersManagedBeanSAP.printsAction()}" />
</p:commandLink>
让我澄清一下 managedbean 方面;
purchaseOrder 对象包括 PO_NUMBER() 我使用 PO_NUMBER() 从 SAP 生成 pdf 文档 (pdfDoc) 作为 ByteArrayOutputStream。使用 for 循环,我尝试生成包含与所选列一样多的 pdf 文档的 zip 文件。顺便说一句,我不确定我做对了。
带“返回(StreamedContent)输出;”代码块我试图返回 zip 文件,但我得到“java.util.zip.ZipOutputStream 无法转换为 org.primefaces.model.StreamedContent”异常。由于
你能帮我解决这个问题吗?
public StreamedContent printsAction()
{
if(!termsAgreed)
RequestContext.getCurrentInstance().execute("PF('warningDialog').show();");
else
{
if (getSelectedPurchaseOrders() != null && !getSelectedPurchaseOrders().isEmpty()) {
try
{
FileOutputStream zipFile = new FileOutputStream(new File("PO_Reports.zip"));
ZipOutputStream output = new ZipOutputStream(zipFile);
for (PurchaseOrderSAP purchaseOrder : getSelectedPurchaseOrders()) {
ByteArrayOutputStream pdfDoc = purchaseOrderSAPService.printOrder(selectedPurchaseOrder.getPO_NUMBER());
ZipEntry zipEntry = new ZipEntry(purchaseOrder.getPO_NUMBER());
output.putNextEntry(zipEntry);
InputStream targetStream = new ByteArrayInputStream(pdfDoc.toByteArray());
IOUtils.copy(targetStream, output);
output.closeEntry();
}
output.finish();
output.close();
return (StreamedContent) output;
}
catch(Exception ex)
{
System.out.println("error when generating...");
ex.printStackTrace();
}
}
}
return null;
}
【问题讨论】:
-
压缩多个 pdf 文件与压缩多个 txt 文件相同,不是 PrimeFaces,而是纯 java。发送一个压缩的 pdf 或普通的非压缩 pdf 或 txt 文件时也会出现错误。尝试...并查看关于如何使用 StreamedContent 的 PF 展示。投射!= 转换
标签: java jsf primefaces