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

前端 未结 1 1496
遥遥无期
遥遥无期 2021-01-24 06:12

I have issue with primefaces 5.0 component p:fileDownload , I have following bean store DefaultStreamedContent , and I get the value stored to download it simply :

相关标签:
1条回答
  • 2021-01-24 07:16

    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>
    
    0 讨论(0)
提交回复
热议问题