Primefaces downloadFile java.io.IOException: Stream Closed after second time

泪湿孤枕 提交于 2019-12-02 07:19:28

I solved issue finally, I make new class store my data, and action that prepare stream to download from my class object, and when download file I read bytes from my class bytes, like the following:

Class:

public class Attachment implements Serializable {

    private String name;
    private String type;
    private String encoding;
    private byte[] data;

    // ... Setters and getters ...

    public Attachment () {
        this.encoding = "UTF-8";
    }

}

Bean:

public class MailBean implements Serializable {
    // alot of properties
    private Attachment attachment;
    // setters and getters
}

Managed Bean :

public class MailManagedBean implements Serializable {
    private DefaultStreamedContent attachmentToDownload;
    private MailBean mail;
    // @ here setter and getter too .

    public void prepare() {
        InputStream stream = new ByteArrayInputStream(mail.getAttachment.getData());
        attachmentToDownload = new DefaultStreamedContent(stream, mail.getAttachment.getType(), mail.getAttachment.getName(), mail.getAttachment.getEncoding());
    }
}

In XHTML file (I added action) :

<p:commandLink ajax="false" value="Download Attachment" 
    rendered="#{mbMail.mail.attachment != null}" action="#{mbMail.prepare()}">
    <p:fileDownload value="#{mbMail.attachmentToDownload}" />
</p:commandLink>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!