我已经实现了一种以这种方式导出PDF文件和一些信息的机制:
public void generatePdf() {
String fileName = "Bolla_" + productionOrder.getOrderNumber();
writePDFToResponse(new GenerateStatusPDF(companyInfo).generate(productionOrder), fileName);
}
private void writePDFToResponse(ByteArrayOutputStream baos, String fileName) {
try {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.responseReset();
externalContext.setResponseContentType("application/pdf");
externalContext.setResponseHeader("Expires", "0");
externalContext.setResponseHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
externalContext.setResponseHeader("Pragma", "public");
externalContext.setResponseHeader("Content-disposition", "attachment, filename=" + fileName + ".pdf");
externalContext.setResponseContentLength(baos.size());
java.io.OutputStream out = externalContext.getResponseOutputStream();
baos.writeTo(out);
externalContext.responseFlushBuffer();
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
}
这是我的.xhtml
文件使用情况:
<h:commandLink>
<p:graphicImage name="/ultima-layout/images/pdf_icon.png"
width="64px" />
<p:fileDownload
value="#{productionOrderStatusView.generatePdf()}" />
</h:commandLink>
我不为什么,但是最近几天我遇到了这个错误的问题:
ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION
我在哪里错了?可能是Google Chrome浏览器中的一些新更新?
答案 0 :(得分:2)
文件名可能带有逗号。
通常在文件名中加上双引号,以避免重复字符,例如(“ Content-Disposition”,“ attachment; filename = \“” + filename +“ \”“)
请查看标准。 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition