File upload 提交到数据库之前,在p:graphicImage中预览图像

File upload 提交到数据库之前,在p:graphicImage中预览图像,file-upload,primefaces,jsf-2.2,graphicimage,File Upload,Primefaces,Jsf 2.2,Graphicimage,在我的应用程序中,我希望用户创建一个人,提交表单,在下一页验证详细信息,然后提交给db。这个人也可以有一个形象。为了简单起见,我只使用了两个字段1)名称2)文件。我无法在“验证”页面上显示图像。bean是会话作用域,但是当FacesContext.getCurrentInstance()不等于PhaseId.RENDER_响应时,文件大小变为0。我是一个JSF新手,即使在理解了使用p:graphicImage时有两个单独的调用之后,也无法理解它 Testfileupload.xhtml如下所示

在我的应用程序中,我希望用户创建一个人,提交表单,在下一页验证详细信息,然后提交给db。这个人也可以有一个形象。为了简单起见,我只使用了两个字段1)名称2)文件。我无法在“验证”页面上显示图像。bean是会话作用域,但是当FacesContext.getCurrentInstance()不等于PhaseId.RENDER_响应时,文件大小变为0。我是一个JSF新手,即使在理解了使用p:graphicImage时有两个单独的调用之后,也无法理解它

Testfileupload.xhtml如下所示

        <h:outputText value="Person Name *" />
        <p:inputText value="#{personBean.name}"
                        required="true" 
                        requiredMessage="You must enter a Business Name"
                        id="name" />
        <p:message for="name" />

        <h:outputLabel for="image" value="Select Picture:" />
        <p:fileUpload id="image" value="#{personBean.file}" mode="simple" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
        <p:message for="image" />

        <p:commandButton value="Submit" action="#{personBean.addverifyBusiness}" ajax="false" /> 
</h:form>
<h:body>
     <h:outputText value="Picture" />
                    <p:graphicImage value="#{personBean.imagestream}" >
                    <f:param name="file" value="#{personBean.person.file}" />
                    </p:graphicImage>


  </h:body>

}

testresult.xhtml如下所示

        <h:outputText value="Person Name *" />
        <p:inputText value="#{personBean.name}"
                        required="true" 
                        requiredMessage="You must enter a Business Name"
                        id="name" />
        <p:message for="name" />

        <h:outputLabel for="image" value="Select Picture:" />
        <p:fileUpload id="image" value="#{personBean.file}" mode="simple" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
        <p:message for="image" />

        <p:commandButton value="Submit" action="#{personBean.addverifyBusiness}" ajax="false" /> 
</h:form>
<h:body>
     <h:outputText value="Picture" />
                    <p:graphicImage value="#{personBean.imagestream}" >
                    <f:param name="file" value="#{personBean.person.file}" />
                    </p:graphicImage>


  </h:body>

结果如下 inside Person setfile上载的文件名为::202.JPG::上载的文件大小::2022045 在getImagestream()内部上载的文件名为::202.JPG::上载的文件大小::2022045 文件不为空 phase=render\u响应上载的文件名为::202.JPG::上载的文件大小::2022045 内部getImagestream()上载的文件名为::202.JPG::上载的文件大小::0 文件不为空 javax.faces.request.charset-UTF-8 personBean-com.myjsf。PersonBean@2784a989

投人之后 在强制转换上载的文件名为::202.JPG后,阶段不呈现\u响应 在强制转换上载的文件大小为::0后,阶段未呈现\u响应 阶段未呈现\u响应上载的文件名为::202.JPG::上载的文件大小::0 2015年3月29日下午5:42:15 org.primefaces.application.PrimeResourceHandler HandlerResourceRequest
严重:流式传输动态资源时出错。读取com.myjsf.PersonBean类型上的“imagestream”时出错

我认为primefaces只能呈现静态内容,如果您使用的是默认的pgraphic图像。如果要在执行任何其他操作之前在验证页面上呈现图像,则需要某种类型的额外组件。我推荐AdvancedGraphicImageRenderer。我将附上下面的链接。这对我来说没有任何问题。代码几乎是自解释的,并且很容易修改


我忘了提到您可能需要创建一个org.primefaces.model.StreamedContent的实现来处理字节数组。如果我没记错的话,我认为默认的流式内容是不可序列化的。我在开发webflow应用程序时遇到了这个错误。如果需要,我可以查一查。我想我仍然有它的代码。

将您的
公共流内容getImagestream(){…}
更改为此

public StreamedContent getImagestream2() {
    if( file != null ){
        return new DefaultStreamedContent(new ByteArrayInputStream(file.getContents()), file.getContentType());
    }else{
        return new DefaultStreamedContent();
    }
}
在结果视图中,使用以下命令:

<p:graphicImage value="#{personBean.imagestream2}" >
                    <f:param name="file" value="#{personBean.person.file.fileName}" />
                </p:graphicImage>

您的图像参数太重