How to use Streamed Content with p:fileDownload to Download Non class path File

前端 未结 1 729
难免孤独
难免孤独 2020-12-16 03:02

I\'m using Primefaces

p:fileDownload

to download a file which is not in class path.
So I\'m passing FileInputStream<

相关标签:
1条回答
  • 2020-12-16 03:18

    The NotSerializableException is thrown because the view scope is represented by the JSF view state which can in turn be serialized to HTTP session in case of server side state saving or a HTML hidden input field in case of client side state saving. The FileInputStream can in no way be represented in a serialized form.

    If you absolutely need to keep the bean view scoped, then you should not be declaring StreamedContent as an instance variable, but instead recreate it in the getter method. True, doing business logic in a getter method is usually frowned upon, but the StreamedContent is a rather special case. In the action method, you should then only prepare serializable variables which are later to be used during DefaultStreamedContent construction.

    @ManagedBean
    @ViewScoped
    public class DownloadBean implements Serializable {
    
        private String path;
        private String contentType;
    
        public void downloadAction() {
            path = "C:/temp.txt";
            contentType = FacesContext.getCurrentInstance().getExternalContext().getMimeType(path);
        }
    
        public StreamedContent getdFile() throws IOException {
            return new DefaultStreamedContent(new FileInputStream(path), contentType);
        }
    
    }
    

    (note that I also fixed your way to get the content type; you have this way much more freedom to configure mime types via <mime-mapping> entries in web.xml)

    The <p:graphicImage> has by the way exactly the same problem with StreamedContent. See also among others Display dynamic image from database with p:graphicImage and StreamedContent.

    0 讨论(0)
提交回复
热议问题