Display an image that is outside the JSF project

Asked

Viewed 2,691 times

0

Hello I have a screen that uploads a file and saves in a folder in a directory of the computer until and saved in the database the complete path of the image, now I want to display this image in a table for example I made the example of the first faces but not conisgo 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 complete example path: D: data Sar uploads Chrysanthemum.jpg

  • 1

    Why use the p:lightBox? Try it without him, and see where it goes.

2 answers

0

That way D:\dados\sar\uploads\ needs to be served by a Servlet, ie if I access http://localhost:8080/app/uploads, should have this directory mapped.

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

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

  • Hello Giuliano I did so

  • File file file = new File("D: Sar uploads data "); Inputstream inputStream = new Fileinputstream(file); Streamedcontent = new Defaultstreamedcontent(inputStream, "image/jpeg", "Chrysanthemum.jpg");

  • but you’re giving access denied

  • SEVERE: Error Rendering View[/restrict/system/room.xhtml] javax.faces.Facesexception: javax.el.Elexception: /restrict/system/room.xhtml @97,157 name="#{salaBean.displayImagem(item)}": java.io.Filenotfoundexception: D: Sar uploads data (Access denied)

  • From what I understand, uploads is a directory, right? Then you must concatenate some parameter passed by the URL that forms the name of a valid file, for example what you quoted Chrysanthemum.jpg, then you would read from D:\dados\sar\uploads\Chrysanthemum.jpg. If the error persists, check if you have access to this directory.

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;
}
  • who is photoPerfil

  • Error in simplifying code for posting. Fixed!

  • @Nilsonuehara, from my experience using the StreamedContent inside the table does not work.

  • @Cold works yes, I have this in datatable and picklist. I think without the cache="disable" property it might go wrong even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.