Ich verwende PrimefacesWie gestreamte Inhalt mit p verwenden: Filedownload-Datei Non Classpath
p zum Download: Filedownload
zum Herunterladen einer Datei, die nicht in Klassenpfad.
So passiere ich FileInputStream als Parameter zu DefaultStreamedContent.
Alles funktioniert gut, wenn meine Bohne bei @SessionScoped ..., gehalten wird
Aber
java.io.NotSerializableException: java.io.FileInputStream
ausgelöst wird, wenn ich behalte meine Bohne in @Viewscoped.
Mein Code:
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>
Gibt es keine Methode, um es funktioniert?
Vielen Dank BelusC. Es hat wie ein Zauber funktioniert. Ich habe die Regel über Instanzvariablen und Serialisierung vergessen. –