我不熟悉Java,但我有一个任务要解决。
我有一个 JSR-286 portlet。有一个 download.jspx 带有一个应该下载 PDF 文件的按钮。该文件由 byte[] 数组中的 API 提供。
我尝试使用 File.createTempFile() 将文件保存在用户磁盘上,但是当应用程序部署在 Weblogic Server(门户)上时,它无法访问文件系统。
如何创建一个从字节数组下载文件的按钮操作?
最后,我找到了从字节数组下载文件的解决方案
在下载.jspx 中:
<af:commandButton text="Печать" id="cbPrint"
styleClass="AFStretchWidth btn btn-default"
inlineStyle="text-align:center; width:100.0px; font-size:14px;margin:10px">
<af:fileDownloadActionListener filename="download.pdf"
contentType="application/pdf"
method="#{RequestBean.downloadReport}"/>
</af:commandButton>
在 RequestBean.java 中
public void downloadReport(FacesContext facesContext,
OutputStream outputStream) {
try {
outputStream.write(getPrintBytes());
outputStream.flush();
outputStream.close();
} catch (Exception e) {
logger.log(Level.SEVERE, "downloadReport", e);
}
facesContext.responseComplete();
}