1

I would like to present an image that comes from an internal image server of the company. I can access it by an internal http address. Must present it without showing this internal address in the source code, using a p.graphicImage .

The View is here:

<p:fieldset legend="Dados pessoais">

<br/>
<p:graphicImage value="#{funcionarioEditMB.graphicText}" id="foto" cache="false">
    <f:param name="id" value="#{funcionarioEditMB.bean.matricula}" />
</p:graphicImage>
<br/>    

//--*-

MB related part:

//no init chamo esta função passando a matricula (this.getImagem(funcionario.getMatricula());)

public void getImagem(Integer matricula){
    ByteArrayOutputStream os = null;
    byte[] bytes = null;
    FacesContext context = FacesContext.getCurrentInstance();
    String id = context.getExternalContext().getRequestParameterMap().get("id");


    try{


        URL url = new URL(funcionarioBC.getImage(Integer.parseInt(id)));
        BufferedImage image = ImageIO.read(url);
        os = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", os);
        bytes = os.toByteArray();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
          graphicText =  new DefaultStreamedContent();
           } else {
            // So, browser is requesting the image. Get ID value from actual request param.
             graphicText =  new DefaultStreamedContent(new ByteArrayInputStream(bytes));
           }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private StreamedContent graphicText;

public StreamedContent getGraphicText() {
    return graphicText;
}

But the image does not appear in the view. The following link is displayed: http://localhost:8080/eSGRH/javax.faces.resource/dynamiccontent.properties.jsf?ln=primefaces&pfdrid=aiHZO8v7zFQ6p%2bPffO1S1vWT2KGBHKr%2bR3guIqtplAUT4IpnxJZmHw==&id=479488&pfdrid_c=false&uid=4e51fb92-b0bc-4530-8e3c-3d28fa750563

Could someone help me to understand what is wrong?

Vasil Lukach
  • 3,383
  • 3
  • 27
  • 36
Rodrigo Ramos
  • 247
  • 2
  • 12
  • Your bean logic is wrong (and inefficient; that Java 2D API is completely unnecessary). This question is a duplicate of http://stackoverflow.com/questions/8207325/display-dynamic-image-from-database-with-pgraphicimage-and-streamedcontent/12452144#12452144 – BalusC May 20 '15 at 20:30
  • I read this in early. But the image do not appear in p:graphicImage – Rodrigo Ramos May 21 '15 at 13:50
  • in URL url = new URL(funcionarioBC.getImage(Integer.parseInt(id))); I use: URL url = new URL(" http://www.saudedica.com.br/wp-content/uploads/2014/03/Pera.jpg "); and no apper img yet – Rodrigo Ramos May 21 '15 at 13:54

0 Answers0