PrimeFaces fileDownload does not work

后端 未结 4 1291
深忆病人
深忆病人 2021-01-02 18:03

I can\'t get the primeFaces work. While I succeed in downloading the file using a custom download bean. Seems like the issu is related to the

相关标签:
4条回答
  • 2021-01-02 18:07
    InputStream stream = this.getClass().getResourceAsStream("sessionlog.csv");
    

    The way as you have located the CSV file expects it to be in the same package as the current class, the FileDownloadBean.

    If it is actually located in the package root, then you should rather be using:

    InputStream stream = this.getClass().getResourceAsStream("/sessionlog.csv");
    

    Or if it is actually located in a different package, for example com.example, then you should rather be using:

    InputStream stream = this.getClass().getResourceAsStream("/com/example/sessionlog.csv");
    

    Or if it is actually located in the root of the public webcontent (there where the /WEB-INF folder also is, among all other web files), then you should rather be using:

    InputStream stream = externalContext.getResourceAsStream("/sessionlog.csv");
    

    (which by the way also works fine for the WAR classes instead of this.getClass())

    0 讨论(0)
  • 2021-01-02 18:14

    My fileuplaad.xhtml is as shown below

      <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui">
    
    
        <h:form id="downloadFileForm">
                <p:commandButton id="downloadLink" value="Download"                                                                             ajax="false"
                                                    onclick="PrimeFaces.monitorDownload(start, stop)"
                                                    icon="ui-icon-arrowthichk-s">
                                                    <p:fileDownload value="#{fileDownloadController.file}" />
                                                </p:commandButton>
    
    
            </h:form>
        </html>
    
    Here is my bean:
    @ManagedBean(name="fileDownloadController")
    public class FileDownloadController implements Serializable {
    
    
        private static final long serialVersionUID = 1L;
    
        private StreamedContent file;  
    
    
        public FileDownloadController() {          
            InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/download/FileName.csv");  
            file = new DefaultStreamedContent(stream, "download/csv", "FileName.csv");  
        }  
    
        public StreamedContent getFile() {  
            return file;  
        }    
    }
    
    0 讨论(0)
  • 2021-01-02 18:28

    I am modify my InputStream the code

    InputStream stream = this.getClass().getResourceAsStream("sessionlog.csv");
    

    change by

    InputStream stream = new FileInputStream(new File("URL"))
    

    it was solution.

    0 讨论(0)
  • 2021-01-02 18:33

    Are you sure that you are successfully loading the CSV file as a resource from the Class? It looks like the InputStream may be null.

    0 讨论(0)
提交回复
热议问题