Dowload File Primerfaces Java

1

I am implementing a code to download with primerfaces a pdf and I get the following error:

  

java.lang.NullPointerException       at org.primefaces.component.filedownload.FileDownloadActionListener.processAction (FileDownloadActionListener.java:89)       at javax.faces.event.ActionEvent.processListener (ActionEvent.java:88)       at javax.faces.component.UIComponentBase.broadcast (UIComponentBase.java:814)       at javax.faces.component.UICommand.broadcast (UICommand.java:300)       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)       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:658)       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:292)       at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:207)       at org.primefaces.webapp.filter.FileUploadFilter.doFilter (FileUploadFilter.java:78)       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:240)       at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:207)       at org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52)       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:240)       at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:207)       at org.apache.catalina.core.StandardWrapperValve.invoke (StandardWrapperValve.java:212)       at org.apache.catalina.core.StandardContextValve.invoke (StandardContextValve.java:106)       at org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:502)       at org.apache.catalina.core.StandardHostValve.invoke (StandardHostValve.java:141)       at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:79)       at org.apache.catalina.valves.AbstractAccessLogValve.invoke (AbstractAccessLogValve.java:616)       at org.apache.catalina.core.StandardEngineValve.invoke (StandardEngineValve.java:88)       at org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:528)       at org.apache.coyote.http11.AbstractHttp11Processor.process (AbstractHttp11Processor.java:1099)       at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process (AbstractProtocol.java:670)       at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun (NioEndpoint.java:1520)       at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run (NioEndpoint.java:1476)       at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:617)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run (TaskThread.java:61)       at java.lang.Thread.run (Thread.java:745)

My code is Next

The java.class bean:

public StreamedContent getFileDownload() {

    InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream
            (ParamSistema.getParametro("rutaPdf") + this.comodato.getNumComodato() + ".pdf");
    System.out.println(ParamSistema.getParametro("rutaPdf") + this.comodato.getNumComodato() + ".pdf");
    fileDownload = new DefaultStreamedContent(stream, "application/pdf",   this.comodato.getNumComodato() + ".pdf");
    return fileDownload;
}
public void setFileDownload(StreamedContent fileDownload) {
    this.fileDownload = fileDownload;
}

HTML Code:

<p:commandButton id="btnPdf" icon="ui-icon-document"
                            onclick="PrimeFaces.monitorDownload(start, stop);"
                            title="Bajar Documento">
                            <p:fileDownload value="#{comodatoBean.fileDownload}" />
                        </p:commandButton>
  

PD: "pathPdf" equals this: C: / certificate /

As far as I understand, although I give a value to InputStream it always remains as null and I do not understand why, try it the way it is in the code and with getClass () . any help is well received in advance thanks.

Try it in the following way:

    InputStream stream = this.getClass().getResourceAsStream(ParamSistema.getParametro("rutaPdf") + this.comodato.getNumComodato() + ".pdf");
    fileDownload = new DefaultStreamedContent(stream, "application/pdf",   this.comodato.getNumComodato() + ".pdf");

and the result was the same.

And this shows in the log:

Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Informatica'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
    
asked by Felipe Rojas Alvares 19.01.2018 в 16:12
source

2 answers

0

What I was missing was to initialize the InputStream variable that would be the first line, the other one is just so you know what is pdf

InputStream is = this.getClass().getResourceAsStream("certificado.pdf");
file = new DefaultStreamedContent(is, "application/pdf", "certificado.pdf");
    
answered by 19.01.2018 в 16:57
0

Solve the problem with the Next Code:

public void prepDownload() throws Exception {
      File file = new File(ParamSistema.getParametro("rutaPdf") + this.comodato.getNumComodato() + ".pdf");
      InputStream input = new FileInputStream(file);
      ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
      setFileDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
}

The problem was that I could not go out and download the file outside my workspace and solve it with that code. For those who commented thank you.

    
answered by 22.01.2018 в 13:14