PrimeFaces fileDownload does not work

会有一股神秘感。 提交于 2019-11-30 13:58:49
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())

dionicio calles

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.

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.

Pravin

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;  
    }    
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!