如何在p:file中使用流式内容下载以下载非类路径文件 [英] How to use Streamed Content with p:fileDownload to Download Non class path File

查看:95
本文介绍了如何在p:file中使用流式内容下载以下载非类路径文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Primefaces

I'm using Primefaces

p:fileDownload

p:fileDownload

下载不在类路径中的文件.
因此,我将 FileInputStream 作为参数传递给 DefaultStreamedContent .
当我的bean保留在 @SessionScoped ...,
时,一切正常 但是

to download a file which is not in class path.
So I'm passing FileInputStream as parameter to DefaultStreamedContent.
Every thing works fine when my bean is kept at @SessionScoped...,
But

java.io.NotSerializableException:java.io.FileInputStream

java.io.NotSerializableException: java.io.FileInputStream

当我将豆放在 @Viewscoped 中时,会抛出

.

is thrown when I keep my bean in @Viewscoped.

我的代码:

DownloadBean.java

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

index.xhtml

<h:form>
    <h:commandLink action="#{downloadBean.downloadAction}">
        Download
        <p:fileDownload value="#{downloadBean.dFile}"/>
    </h:commandLink>
</h:form>

没有任何方法可以使其正常工作吗?

Isn't there any method to make it work?

推荐答案

之所以抛出NotSerializableException,是因为视图范围由JSF视图状态表示,如果保存了服务器端状态,则该视图状态又可以序列化为HTTP会话.或在保存客户端状态的情况下提供HTML隐藏的输入字段. FileInputStream决不能以序列化形式表示.

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.

如果绝对需要使Bean视图保持作用域,那么您不应将StreamedContent声明为实例变量,而应在getter方法中重新创建它.的确,通常不赞成使用getter方法进行业务逻辑,但是StreamedContent是一个非常特殊的情况.在action方法中,您只应准备可序列化的变量,这些变量稍后将在DefaultStreamedContent构造期间使用.

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);
    }

}

(请注意,我还固定了获取内容类型的方式;通过这种方式,您可以通过web.xml中的<mime-mapping>条目配置mime类型的自由度更大)

(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)

<p:graphicImage>StreamedContent完全一样.另请参见使用p:graphicImage和StreamedContent显示来自数据库的动态图像.

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.

这篇关于如何在p:file中使用流式内容下载以下载非类路径文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆