Jsf PrimeFaces文件下载不起作用
我无法获取primeFacesJsf PrimeFaces文件下载不起作用,jsf,download,primefaces,java-ee-5,Jsf,Download,Primefaces,Java Ee 5,我无法获取primeFaces是否确定已成功将CSV文件作为类中的资源加载?看起来InputStream可能为空 您定位CSV文件的方式期望它与当前类FileDownloadBean位于同一个包中 如果它实际上位于包根目录中,那么您应该使用: InputStream stream = this.getClass().getResourceAsStream("/sessionlog.csv"); InputStream stream = this.getClass().getResourceAs
是否确定已成功将CSV文件作为类中的资源加载?看起来InputStream可能为空
您定位CSV文件的方式期望它与当前类FileDownloadBean
位于同一个包中
如果它实际上位于包根目录中,那么您应该使用:
InputStream stream = this.getClass().getResourceAsStream("/sessionlog.csv");
InputStream stream = this.getClass().getResourceAsStream("/com/example/sessionlog.csv");
InputStream stream = externalContext.getResourceAsStream("/sessionlog.csv");
或者,如果它实际位于不同的包中,例如com.example
,则您应该使用:
InputStream stream = this.getClass().getResourceAsStream("/sessionlog.csv");
InputStream stream = this.getClass().getResourceAsStream("/com/example/sessionlog.csv");
InputStream stream = externalContext.getResourceAsStream("/sessionlog.csv");
或者,如果它实际上位于公共webcontent的根目录中(在所有其他WEB文件中,/WEB-INF
文件夹也位于该目录中),那么您应该使用:
InputStream stream = this.getClass().getResourceAsStream("/sessionlog.csv");
InputStream stream = this.getClass().getResourceAsStream("/com/example/sessionlog.csv");
InputStream stream = externalContext.getResourceAsStream("/sessionlog.csv");
(顺便说一句,这也适用于WAR类,而不是this.getClass()
)我正在修改输入流代码
InputStream stream = this.getClass().getResourceAsStream("sessionlog.csv");
改变
InputStream stream = new FileInputStream(new File("URL"))
这是一个解决方案。我的文件uplaad.xhtml如下所示
<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;
}
}
这是我的豆子:
@ManagedBean(name=“fileDownloadController”)
公共类FileDownloadController实现可序列化{
私有静态最终长serialVersionUID=1L;
私有流内容文件;
公共文件下载控制器(){
InputStream stream=((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(“/download/FileName.csv”);
文件=新的默认流内容(流,“下载/csv”,“文件名.csv”);
}
公共流内容getFile(){
返回文件;
}
}
@balusC确实,抛出错误是因为filedownloader找不到文件,然后返回空值。我将文件的位置更改为同一个类包,现在工作正常。确切地说,这最终是一个路径问题。严重的个人疏忽。我在一个基于Debian的Linux下,所以路径有时可能会变得棘手,这就是为什么我简单地使用“文件名”。再次感谢您的宝贵说明。使用ClassLoader#getResourceAsStream()
时,您应该考虑相对于类路径根(即包根)。使用ExternalContext#getResourceAsStream()
时,您应该考虑相对于webapp根目录(即WebContent文件夹)。当您想直接从本地磁盘文件系统获取它们时,您应该使用带有绝对磁盘文件系统路径的FileInputStream
。我的问题是,当通过HttpsessionListener的sessionCreated()方法创建文件时,file file file=new file(“sessionlog.csv”);它存储在/home/mediterran/glassfish/glassfish/domains/domain1/sessionlog.csv中,因此实际上,我不知道如何指示路径,以便将文件写入到与/WEB inf相同的根目录中。您应该真正避免在webapplications中使用带有相对路径的文件。相对路径取决于服务器的启动方式。另请参见我对以下问题的回答:以及