Display an image that is outside the JSF project

0

Hello, I have a screen that uploads a file and saves it to a folder in a computer's directory, and saves the full image path in the database, now I want to display the image in a table, for example I did the example of the firstfaces but I can not display the image.

<p:column headerText="Foto">
     <p:lightBox styleClass="imagebox" id="lighbox1">  
          <h:outputLink value="#{item.imagem}" title="Nature 1">  
           <h:graphicImage name="#{salaBean.exibirImagem(item)}" id="nature1" style="height: 77px; width: 100px" />  
           </h:outputLink>
       </p:lightBox>
 </p:column>

In my bean I bring the full path example: D: \ data \ sar \ uploads \ Chrysanthemum.jpg

    
asked by anonymous 26.09.2014 / 19:46

2 answers

0

You can create a StreamedContent to traffic your photo.

<p:graphicImage value="#{fotoMB.foto}" cache="disable"/>
public StreamedContent getFoto(){
    File foto=new File("suafoto.jpg");
    DefaultStreamedContent content=null;
    try{
        BufferedInputStream in=new BufferedInputStream(new FileInputStream(foto));
        byte[] bytes=new byte[in.available()];
        in.read(bytes);
        in.close();
        content=new DefaultStreamedContent(new ByteArrayInputStream(bytes),"image/jpeg");
    }catch(Exception e){
        log.error(e);
    }
    return content;
}
    
26.09.2014 / 20:02
0

This D:\dados\sar\uploads\ path must be served by a Servlet, that is, if I access http://localhost:8080/app/uploads , you should have this mapped directory.

After request of the browser, read the path of the disk and return in response.getOutputStream() , not forgetting to set the Content-Type to image\jpeg .

To read disk image use ImageIO or Apache Commons-IO .

    
26.09.2014 / 19:59