录制控制台图像 graphicImage Primefaces 时出错?

Error when recording console image graphicImage Primefaces?

如上所述解决了使用组件 PrimeFaces“graphicImage”从另一个控制器加载图像时的问题我现在遇到的问题是当我将图像刻录到本地地址时,我生成了一个 PrimeFaces 错误,但显然不会影响我当前的功能。

图像加载使 setAddAttributes 方法,问题是我注意到当我转到方法时记录 setSaveAttributes 发生了什么顺利地记录了我的数据,但在控制台中留下了这个异常,我认为它必须执行该实用程序是 SessionScoped 管理(但它不支持不识别我并且我管理生成 null)。

<p:graphicImage value="#{mGraficoImagen.graphicImage}" cache="false" />

主管理控制器:

@ManagedBean
@ViewScoped
public class CMantConsultaProductosList {

    @ManagedProperty(value="#{mGraficoImagen}")
    private MGraficoImagen mGraficoImagen;  

    private MProductoBean mantForm;

    @PostConstruct
    public void setViewAtributes() {
        MProductoBean mantForm = new MProductoBean();
        mantForm.setVnomImagen("fancoil-fn.png");
    }


    public void setAddAttributes(ActionEvent e) {
        StringBuilder archivoImagen = new StringBuilder();
        archivoImagen.append("C://images//");
        archivoImagen.append(mantForm.getVnombreImagen());      
        this.mGraficoImagen.setGraphicImage(new DefaultStreamedContent(new FileInputStream(archivoImagen.toString()),
                                            "image/png",
                                             mantForm.getVnombreImagen()));
    }

    public void setSaveAttributes(ActionEvent e)  {
         .........

         iProductoService.grabarProducto(mantBean);
   }

    /* get and set */
}

使用管理:

@SessionScoped
@ManagedBean(name = "mGraficoImagen")
public class MGraficoImagen implements Serializable {

    private static final long serialVersionUID = 1L;
    private StreamedContent graphicImage;

    /**
     * @return the graphicImage
     */
    public StreamedContent getGraphicImage() {
        return graphicImage;
    }

    /**
     * @param graphicImage the graphicImage to set
     */
    public void setGraphicImage(StreamedContent graphicImage) {
        this.graphicImage = graphicImage;
    }

}

错误控制台:

feb 04, 2015 5:11:44 PM org.primefaces.application.resource.StreamedContentHandler handle
Grave: Error in streaming dynamic resource. Stream Closed
feb 04, 2015 5:11:44 PM org.apache.catalina.core.StandardWrapperValve invoke
Grave: Servlet.service() para servlet Faces_Servlet lanzó excepción
java.io.IOException: java.io.IOException: Stream Closed
    at org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:78)
    at org.primefaces.application.resource.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:72)
    at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:153)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:72)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.sgkyros.common.filter.EncodingFilter.doFilter(EncodingFilter.java:36)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.io.IOException: Stream Closed
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:243)
    at org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:68)

一个InputStream只能读一次(然后会被关闭,从而腾出昂贵的文件资源)。但是您似乎将其分配为会话作用域 bean 的 属性,这意味着它将被多次读取。

这都是错的。您应该使用无状态 bean,它在 getter 方法内的 方法局部范围 中创建并保存 StreamedContent 实例。你永远不应该将它分配为一个 bean 属性 并且只在浏览器实际请求图像时才创建它。您可以通过嵌套在 <p:graphicImage>.

中的 <f:param> 作为请求参数传递图像标识符(例如文件名或 DB PK),从而使其动态化

This was fleshed out and explained so many times before, among others in this answer: Display dynamic image from database with p:graphicImage and StreamedContent.