我使用HttpURLConnection下載文件,然後我想給用戶下載該文件的選項。我使用這個primefaces示例http://www.primefaces.org/showcase/ui/file/download.xhtml。Primefaces文件下載只適用於我使用默認構造函數
我的問題是,如果我通過默認的構造函數下載硬編碼文件,就像它在示例中提到的那樣,一切正常。但如果我將文件名傳遞給接受param的構造函數,我會得到一個空指針。
下面是這兩個構造函數的代碼(僅使用硬編碼的文件工作的默認構造函數)
@ManagedBean
public class FileDownloadView {
private StreamedContent file;
private InputStream stream;
private String fileName;
public String getFileName() {
return fileName;
}
public FileDownloadView() {
InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/tmp/1kb.txt");
file = new DefaultStreamedContent(stream, "text/plain", "text.txt");
System.out.println("fileName......." + "test.txt");
}
public FileDownloadView(String fileName) {
this.fileName = fileName;
InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/tmp/" + fileName);
this.fileName = fileName;
file = new DefaultStreamedContent(stream, "text/plain", fileName);
System.out.println("fileName......." + file.getName());
}
public StreamedContent getFile() {
System.out.println("file "+file.getName());
return file;
}
}
在這裏,我是多麼拉文件
<p:commandButton value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop);" icon="ui-icon-arrowthick-1-s">
<p:fileDownload value="#{fileDownloadView.file}" />
</p:commandButton>
<p:outputLabel value="#{fileDownloadView.fileName}"/>
</h:form>
<script type="text/javascript">
function start() {
PF('statusDialog').show();
}
function stop() {
PF('statusDialog').hide();
}
</script>
誰去調用構造函數?不是JSF。 – EJP