Download of pdf files for PF 6.2/JSF 2.2 doesn't work

UI Components for JSF
Post Reply
JavaRookie
Posts: 1
Joined: 12 Jan 2019, 12:25

12 Jan 2019, 13:26

Hello,

Web project effected in the following technology :

[*] Win 7 HE 64 bit
[*] WildFly Full 12.0.0.Final (WildFly Core 4.0.0.Final) - installed as standalone server in my PC disk : E:\jboss\
[*] java.runtime.version = 1.8.0_162-b12
[*] MySQL 5.5.60 Community Server
[*] Hibernate Core {5.1.10.Final}
[*] JSF 2.2
[*] PrimeFaces ver.6.2

Functionality deployed short description and the problem detected :

The pdf files of database registered customers are uploaded onto server to directory as folllows : E:\files\
but in database MySQL table are stored String file names adequate for the customer concerned
.
After logging to application managment console i try to download onto local disk a file of selected customer form
E:\files\*.pdf an error in RT occurs as follows :

2019-01-12 11:22:42,178 INFO [org.primefaces.showcase.view.file.FileDownloadView] (default task-5) String file name of customer selected apperars to bo as :1_Java_wyklad_8.pdf but full filepath appears to be as :E:\files\1_Java_wyklad_8.pdf
2019-01-12 11:22:42,214 FATAL [javax.enterprise.resource.webcontainer.jsf.context] (default task-5) JSF1073: javax.faces.FacesException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=, Message=null
2019-01-12 11:22:42,217 FATAL [javax.enterprise.resource.webcontainer.jsf.context] (default task-5) 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:658)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:67)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at .....etc
Caused by: java.lang.NullPointerException
at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:90)
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)
... 45 more


An extract from xhtml file downoalding the files concerned :

Code: Select all

 <h:outputText value="Download File :" />
                    <p:commandButton value="Download Document Stored" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop);" icon="ui-icon-arrowthick-1-s">
                           [b] <p:fileDownload value="#{fileDownloadView.file}"/>[/b]
                    </p:commandButton>
                    
Bean :

Code: Select all

@ManagedBean
public class FileDownloadView{


    public final static Logger logger = Logger.getLogger(FileDownloadView.class.getName());

    public static String filedownloaded , contentparam , path;


    public RepositoryFileService getRepositoryFileService() {
        return repositoryFileService;
    }

    public void setRepositoryFileService(RepositoryFileService repositoryFileService) {
        this.repositoryFileService = repositoryFileService;
    }

    @ManagedProperty("#{dtGrantEditView}")
   private RepositoryFileService repositoryFileService;

  @PostConstruct
    public void init()
    {
        filedownloaded = repositoryFileService.showSelectedRepoFile();

            contentparam = "application/pdf";

                // path = "/resources/file/"+filedownloaded;

              path=getDisk() + File.separator +"files"+File.separator+filedownloaded;

        logger.log(Level.INFO,"String file name of customer selected apperars to bo as :"+filedownloaded+" but full filepath appears to be as :"+path);

                    InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(path);

                        file = new DefaultStreamedContent(stream, contentparam, filedownloaded);

    }


    public void setFile(StreamedContent file) {

       this.file = file;
    }
            private StreamedContent file;

                        public StreamedContent getFile()
                        {
                            return file;
                        }
}
Remark :

Actual set stored files directory as : path="E:\files\"
.
When I redeploy project setting stored files directory as : path = "/resources/file/"+filedownloaded,
then everything work propertly, but there is no possibility storing the files onto internal directory of project and redeploy it
afterwards (!)
.
Probably there is an pretty problem with 'CanonicalPath' of stored files with PF class (?).
.
Where I commit an error in implementation of PF functionality for downloadin files from server ?
I will be very thankfull for any indication or solution :)
Last edited by JavaRookie on 18 Jan 2019, 16:25, edited 1 time in total.

kukeltje
Expert Member
Posts: 9317
Joined: 17 Jun 2010, 13:34
Location: Netherlands

14 Jan 2019, 16:53

Please edit your question and use [ code ] .... [ /code ] tags without the spaces and use good indentation

And the solution is in no way JSF or PDF related but a plain java one. 'getResourceAsStream(...)' never takes absolute files/paths from the file system as a parameter but relative ones to the classloader (or absolute when starting with a / but always taking the classloader as 'root', not the filesystem)
Ronald van Kuijk
______________________________
PrimeFaces 6.1, PrimeFaces plus 0.0.2 | JbossWildfly 10.1| Mojarra 2.2.13
Fedora 61, Firefox 'most recent'
Read the forum posting rules
Beginners: https://jsf.zeef.com/bauke.scholtz

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 5 guests