I can\'t get the primeFaces
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()
)
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;
}
}
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.