我使用Primefaces如何使用流式內容與號碼:fileDownload下載非類路徑文件
號碼:fileDownload
下載一個文件,該文件是不是在類路徑中。
所以我通過FileInputStream作爲參數DefaultStreamedContent。
每一件事情時,工作我豆保持在@SessionScoped ...精細,
但
java.io.NotSerializableException:java.io.FileInputStream中
被拋出時,我保持我的豆在@Viewscoped。
我的代碼:
DownloadBean.java
@ManagedBean
@ViewScoped
public class DownloadBean implements Serializable {
private StreamedContent dFile;
public StreamedContent getdFile() {
return dFile;
}
public void setdFile(StreamedContent dFile) {
this.dFile = dFile;
}
/**
* This Method will be called when download link is clicked
*/
public void downloadAction()
{
File tempFile = new File("C:/temp.txt");
try {
dFile = new DefaultStreamedContent(new FileInputStream(tempFile), new MimetypesFileTypeMap().getContentType(tempFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
的index.xhtml
<h:form>
<h:commandLink action="#{downloadBean.downloadAction}">
Download
<p:fileDownload value="#{downloadBean.dFile}"/>
</h:commandLink>
</h:form>
難道沒有使它工作的任何方法?
非常感謝BelusC。它像一個魅力。我忘了關於實例變量和序列化的規則。 –