-1

Hi community I have a problem to load an image from a local directory. I relied on as I download a file from a local path, but in this case an image to show in a local directory. Outside the context of the project: C:\\image\\fancoil-pared.ice.png

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

manage:

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

}

Error console:

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)

Hope you can help me with this problem, thanks.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Hernando
  • 83
  • 2
  • 15
  • Instead if you are open to specify a location where the container should serve images/files, you can just provide a `` a relative URL to that location. In the current scenario, it fails to obtain `StreamedContent` because it will obviously no longer be available on the next request to download the image hence, the error : `Error in streaming dynamic resource. null`. In addition to that, you are nowhere returning a default stub (such as `return new DefaultStreamedContent();`) so that it can generate a right URL from where an image is to be downloaded when the browser requests it – Tiny Feb 03 '15 at 18:48
  • and I can do in that friend, the image I have in a separate directory project ...? – Hernando Feb 03 '15 at 19:05
  • Yes you can. "*How*" is fully dependent upon the container you are using. If it is GlassFish, for example then, it can be specified in `glassfish-web.xml` file such as, ``. [See](http://stackoverflow.com/a/19142316/1391249). Please do not consider it a milestone. Everything is fully transparently dependent upon your functional requirements. – Tiny Feb 03 '15 at 19:16
  • You seem to have assumed that images are inline in the HTML output which is not true. See [this answer](http://stackoverflow.com/a/12452144/1391249) which covers everything about how it actually works, "*images are not "inlined" in HTML output, but they are instead requested separately*". – Tiny Feb 03 '15 at 19:20

3 Answers3

0

Try this:

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"
    );
}
Paul Vargas
  • 38,878
  • 15
  • 91
  • 139
0

As Oleg Tandrashko wrote in the PF forum

ViewScoped beans are not supported - the content will be received via an own request which is indepent from the current view.

Kukeltje
  • 11,924
  • 4
  • 19
  • 44
  • I just changed bySessionScoped, but still the same error ...? – Hernando Feb 03 '15 at 22:31
  • Sorry, it should have been a comment, not an answer. Mea culpa. There is more to it. See the comment by @Tiny that links to a post by BalusC – Kukeltje Feb 03 '15 at 22:34
0

Thanks friend and I found the solution, if it is true that I had to separate the load into another manage, I share my code to the community.

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

}
Hernando
  • 83
  • 2
  • 15