如何使用Primefaces中的graphicImage从目录加载图像?

时间:2015-02-03 17:15:25

标签: jsf primefaces

您好社区我从本地目录加载图片时遇到问题。 我依赖于从本地路径下载文件,但在这种情况下是一个要在本地目录中显示的图像。 在项目背景之外:C:\\image\\fancoil-pared.ice.png

<p:graphicImage value="#{cMantConsultaProductosList.graphicImage}" />

管理:

@ManagedBean
@ViewScoped
public class CMantConsultaProductosList {

    private StreamedContent graphicImage;

    @PostConstruct
    @Override
    public void setViewAtributes() {

        InputStream stream = new FileInputStream(new File("C:\\imagen\\fancoil-pared.ice.png"));  
        this.graphicImage = new DefaultStreamedContent(stream, "image/png", "fancoil-pared.ice.png");

    }

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

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

}

错误控制台:

org.primefaces.application.resource.StreamedContentHandler handle
Grave: Error in streaming dynamic resource. null
org.apache.catalina.core.StandardWrapperValve invoke
Grave: Servlet.service() para servlet Faces_Servlet lanzó excepción
java.io.IOException: java.lang.NullPointerException
    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)

希望你能帮我解决这个问题,谢谢。

3 个答案:

答案 0 :(得分:0)

试试这个:

public StreamedContent getGraphicImage() {
    return new org.primefaces.model.DefaultStreamedContent(
        new java.io.FileInputStream("C:\\imagen\\fancoil-pared.ice.png"),
        "image/png",
        "fancoil-pared.ice.png"
    );
}

答案 1 :(得分:0)

Oleg Tandrashko在PF forum

中写道

不支持ViewScoped bean - 内容将通过自己的请求接收,该请求与当前视图无关。

答案 2 :(得分:0)

感谢朋友和我找到了解决方案,如果我不得不将负载分成另一个管理,我将我的代码分享给社区。

@ManagedBean
public class CGraficoImagen {

    public StreamedContent graphicImage;

    public StreamedContent getGraphicImage() throws FileNotFoundException {
        CMantConsultaProductosList rn = getCMantConsultaProductosList();
        MProductoBean mantForm = (MProductoBean) rn.getFormMantenimiento();
        StringBuilder archivoImagen = new StringBuilder();
        archivoImagen.append(ReadParameterProperties.getRutaDirectorioImagen());
        archivoImagen.append(mantForm.getVnomImagen());     
        return new DefaultStreamedContent(new FileInputStream(archivoImagen.toString()),
                                          Constantes.MIME_IMAGE_PNG,
                                          mantForm.getVnomImagen());
    }

    public void setGraphicImage(StreamedContent graphicImage) {
        this.graphicImage = graphicImage;
    }

    private CMantConsultaProductosList getCMantConsultaProductosList() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        return (CMantConsultaProductosList)ctx.getViewRoot().getViewMap().get("cMantConsultaProductosList");
    }

}