Search code examples
javapdfservletsportletjsr286

How to download a pdf file from byte array in Java portlet (JSR-286)?


I am not familiarize with Java, but I have a task to solve.

I have a JSR-286 portlet. There is a download.jspx with a button that should download a PDF file. The file is served from an API in byte[] array.

I've tried to save the file on the user disk with File.createTempFile(), but when application is deployed on the Weblogic Server (portal), it doesn't have access to file system.

How to create a button action that will download the file from byte array?


Solution

  • Finally, I found a solution to download file from a byte array

    In download.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>
    

    In 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();
        }