Java Primefaces文件下载仅在使用默认构造函数时有效
我使用HttpURLConnection下载一个文件,然后我想让用户选择下载该文件。我用这个例子 我的问题是,如果我通过默认构造函数下载硬编码文件(如示例中所述),一切都正常。但是,如果我将文件名传递给接受param的构造函数,就会得到一个空指针 下面是包含两个构造函数的代码(只有包含硬编码文件的默认构造函数有效) 这是我如何提取文件的Java Primefaces文件下载仅在使用默认构造函数时有效,java,jsf,jsf-2,primefaces,Java,Jsf,Jsf 2,Primefaces,我使用HttpURLConnection下载一个文件,然后我想让用户选择下载该文件。我用这个例子 我的问题是,如果我通过默认构造函数下载硬编码文件(如示例中所述),一切都正常。但是,如果我将文件名传递给接受param的构造函数,就会得到一个空指针 下面是包含两个构造函数的代码(只有包含硬编码文件的默认构造函数有效) 这是我如何提取文件的 函数start(){ PF('statusDialog').show(); } 函数停止(){ PF('statusDialog').hide(); } 您
函数start(){
PF('statusDialog').show();
}
函数停止(){
PF('statusDialog').hide();
}
您不能在带有注释的类中创建第二个构造函数来初始化文件名变量。您可以使用将文件名从UI传递到控制器并调用下载
根据标签的定义,它提供了一个选项,可以通过动作监听器将属性值传递给组件,或将参数传递给组件。
因此,在您的情况下,您希望将文件名传递给控制器并相应地下载文件
<p:commandButton value="Download" ajax="true" actionListener="#{fileDownloadView.prepareToDownload}" icon="ui-icon-arrowthick-1-s">
<p:fileDownload value="#{fileDownloadView.file}" />
<f:attribute name="fileName" value="#{fileDownloadView.fileName}" />
</p:commandButton>
你可以试试这个
@ManagedBean
public class FileDownloadView {
private StreamedContent file;
private InputStream stream;
private String fileName = "error";
public String getFileName() {
return fileName;
}
public void setFileName(String _filename) {
this.fileName = _fileName;
}
public FileDownloadView() {
}
prepareToDownload方法允许管理文件下载。通过这种方式,我们可以恢复文件名
public void prepareToDownload(ActionEvent actionEvent){
setFileName((String)actionEvent.getComponent().getAttributes().get("fileName"));
InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/tmp/" + fileName);
setFile( new DefaultStreamedContent(stream, "text/plain", fileName));
}
public StreamedContent getFile() {
System.out.println("file "+file.getName());
return file;
}
public void setFile(StreamedContent _file) {
this.file = _file;
}
}
}
然后在xhtml文件中:
<h:commandLink id="downloadLink"
title="Download"
actionListener="#{fileDownloadView.prepareToDownload}">
<p:graphicImage value="/resources/common/images/download.gif"
alt="Download" />
<f:attribute name="fileName" value="#{fileDownloadView.fileName}" />
<p:fileDownload value="#{fileDownloadView.file}" />
</h:commandLink>
<h:outputText value="#{fileDownloadView.fileName}"/>
谁将调用构造函数?不是JSF。我不明白这一行。你能提供更多的细节吗?我用我的控制器名称更改了viewEDI。但不知道如何使用f:attributeSorry。我已经复制粘贴了我在projectv中使用的代码。现在我更新了我的答案。你也可以尝试同样的方法。感谢详细的解释,我发布了另一个关于为什么按钮不能在Datatable中触发的问题。但如果它不在数据表中,它就可以工作。如果你能看一下,请告诉我。再次感谢!!!那么,我的问题的解决方案是什么呢。如何完成我的任务?通过传递参数下载文件?
public void prepareToDownload(ActionEvent actionEvent){
String fileName = (String)actionEvent.getComponent().getAttributes().get("fileName");
InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/tmp/" + fileName);
file = new DefaultStreamedContent(stream, "text/plain", fileName);
}
@ManagedBean
public class FileDownloadView {
private StreamedContent file;
private InputStream stream;
private String fileName = "error";
public String getFileName() {
return fileName;
}
public void setFileName(String _filename) {
this.fileName = _fileName;
}
public FileDownloadView() {
}
public void prepareToDownload(ActionEvent actionEvent){
setFileName((String)actionEvent.getComponent().getAttributes().get("fileName"));
InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/tmp/" + fileName);
setFile( new DefaultStreamedContent(stream, "text/plain", fileName));
}
public StreamedContent getFile() {
System.out.println("file "+file.getName());
return file;
}
public void setFile(StreamedContent _file) {
this.file = _file;
}
}
}
<h:commandLink id="downloadLink"
title="Download"
actionListener="#{fileDownloadView.prepareToDownload}">
<p:graphicImage value="/resources/common/images/download.gif"
alt="Download" />
<f:attribute name="fileName" value="#{fileDownloadView.fileName}" />
<p:fileDownload value="#{fileDownloadView.file}" />
</h:commandLink>
<h:outputText value="#{fileDownloadView.fileName}"/>