1

I am working on a JSF/PrimeFaces project where I am generating some files from a database and placing them in the web/resources folder of my project.I am using a p:datable to load the file names dynamically into a web page. The datatable has a singleMode activated. What I want is to make the files downloadable based on the users choice.when a file is selected and the download button is pushed an exception is thrown;

Here are the codes I used:

JSF page

   <div id="content">
        <h:form id="resultForm">
            <center>
                <p:panel id="files" style="width: 80%">
                    <p:dataTable id="resultDT" var="file" value="#{gen.outFileNames}" 
                                 selection="#{downloader.selectedFileName}" selectionMode="single" rowKey="#{file}">
                        <f:facet name="header">
                            Generated Files
                        </f:facet>
                        <p:column headerText="File names">
                            <h:outputText value="#{file}" />
                        </p:column>
                        <f:facet name="footer">
                            <p:commandButton value="Download" ajax="false" icon="ui-icon-arrowthick-1-s">
                                <p:fileDownload value="#{downloader.file}"/>
                            </p:commandButton>
                        </f:facet>
                    </p:dataTable>
                </p:panel>
            </center>
        </h:form>
    </div>

Download Controller

@ManagedBean(name = "downloader")
@ViewScoped
public class FilesDownloadController implements Serializable {

    private String selectedFileName;

    public StreamedContent getFile() {
        if (selectedFileName == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Download warning", "Please click a file and click on the download button."));
            return null;
        }
        System.out.println("Download 1 : selectedFileName = " + selectedFileName);
        System.out.println("Download 2");

        System.out.println("Download 3");
        InputStream stream = this.getClass().getResourceAsStream(selectedFileName);
        System.out.println("Download 4");
        StreamedContent file = new DefaultStreamedContent(stream, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getMimeType(selectedFileName), 
        selectedFileName.split("/")[selectedFileName.split("/").length - 1]);
        System.out.println("Download 5 = " + selectedFileName.split("/")[selectedFileName.split("/").length - 1]);
        System.out.println(file != null ? "File content OK " + file.getContentType() + " " + file.getName() : "File content NOK");
        return file;
    }

    public String getSelectedFileName() {
        return selectedFileName;
    }

    public void setSelectedFileName(String selectedFileName) {
        this.selectedFileName = selectedFileName;
        System.out.println("Selected File changed: " + this.selectedFileName );
    }
}

Generator bean

@ManagedBean(name = "gen")
@SessionScoped
public class GeneratorController implements Serializable {

private List<String> outFileNames;

public GeneratorController() {
    outFileNames = new ArrayList<String>();
    outFileNames.add("/resources/images/optimus.jpg");
    outFileNames.add("/resources/images/optimus_1.jpg");
    outFileNames.add("/resources/images/optimus_2.jpg");
    outFileNames.add("/resources/images/optimus_3.jpg");
}

public List<String> getOutFileNames() {
    return outFileNames;
}

public void setOutFileNames(List<String> outFileNames) {
    this.outFileNames = outFileNames;
}
}

Exceptions thrown

        Infos:   Selected File changed: /resources/images/optimus_1.jpg
        Infos:   Download 1 : selectedFileName = /resources/images/optimus_1.jpg
        Infos:   Download 2
        Infos:   Download 3
        Infos:   Download 4
        Infos:   Download 5 = optimus_1.jpg
        Infos:   File content OK image/jpeg optimus_1.jpg
        FATAL:   JSF1073 : javax.faces.FacesException intercepté durant le      traitement de INVOKE_APPLICATION 5 : UIComponent-ClientId=, Message=null
        FATAL:   No associated message
javax.faces.FacesException
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:89)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
       at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
       at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
       at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
       at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
       at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
       at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
       at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
       at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
       at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
       at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:81)
       at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
       at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
       at javax.faces.component.UICommand.broadcast(UICommand.java:300)
       at javax.faces.component.UIData.broadcast(UIData.java:1108)
       at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
       at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
       at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    ... 31 more
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
cdaiga
  • 4,318
  • 3
  • 17
  • 34
  • A guess: remove the first "/" from the filename. It means the name is absolute – Jaqen H'ghar Oct 01 '15 at 18:59
  • Bad idea: _"I am generating some files from a database and placing them in the web/resources folder of my project."_ You should never write into the folder where your war is deployed. – Kukeltje Oct 01 '15 at 19:01
  • And your title is 'wrong'. It should be 'NullPointerException thrown when downloading a file via p:fileDownload' – Kukeltje Oct 01 '15 at 19:17
  • Thanks @Kukeltje for the correction. How do I download a file out of the war file via p:filedownload? – cdaiga Oct 02 '15 at 06:36
  • Thanks @Jaqen H'ghar I have done that but I still have the same problem. – cdaiga Oct 02 '15 at 06:46

2 Answers2

1

If you have an external file somewhere in the file system, you have to use new FileInputStream(new File(exportFile.getAbsolutePath())) instead of FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(exportFile.getAbsolutePath()) in the showcase. I had the same problem in Primefaces 8 and solved it like this:

file = DefaultStreamedContent.builder().name(exportFile.getName())
                  .contentType("application/zip").stream(() ->
      {
         try{
            return new FileInputStream(new File(exportFile.getAbsolutePath()));
                     } catch (FileNotFoundException e) {
                        e.printStackTrace();
                     }
                     return null;
                  }).build();
chux
  • 361
  • 2
  • 4
0

I finally got a way to download the files. The folder where the images are located is not on the class path, so

InputStream stream = this.getClass().getResourceAsStream(selectedFileName);

returns null and an NPE occurs. So by replacing the code of the Download controller with

InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(selectedFileName);

The webapp context is used for locating the files and it works

Kukeltje
  • 11,924
  • 4
  • 19
  • 44
cdaiga
  • 4,318
  • 3
  • 17
  • 34
  • 1
    Keep in mind that this will fail again if you move the files to an external folder (which you should imo) – Kukeltje Oct 02 '15 at 10:49
  • 1
    And, also when the server is configured to not expand WAR on disk, but instead on memory. Food for read: http://stackoverflow.com/questions/18664579/recommended-way-to-save-uploaded-files-in-a-servlet-application and http://stackoverflow.com/questions/25430677/store-pdf-for-a-limited-time-on-app-server-and-make-it-available-for-download/ – BalusC Oct 02 '15 at 10:57