JSF Open Image (Defaultstreamedcontent) on another screen

Asked

Viewed 331 times

0

Hello, I am working on a project with jsf and primefaces, where I have an entity called Registry that has a list of images, these images are in a directory outside the project, which access to load the images and display on the screen, I load these images as byte[] and display on screen as Defaultstreamedcontent with the following structure:

xhtml:

<p:repeat var="foto" value="#{registroMB.registro.fotosAsList}">

        <h:link value="#{imagemMB.foto}" target="_blank">
            <p:graphicImage style="margin-top:10px;margin-bottom:20px;"
                class="col-md-6 col-sm-8 col-xs-12 " value="#{imagemMB.foto}" >
                <f:param name="nomeFoto" value="#{foto.nome}" />
                <f:param name="dataRegistro" value="#{registroMB.registro.data.timeInMillis}" />
            </p:graphicImage>   
        </h:link>           
    </p:repeat>

Imagemmb

public DefaultStreamedContent getFoto(){
    FacesContext context = FacesContext.getCurrentInstance();
    String nomeFoto =  context.getExternalContext().getRequestParameterMap().get("nomeFoto");
    String dataMillis = context.getExternalContext().getRequestParameterMap().get("dataRegistro");
    Calendar c = GregorianCalendar.getInstance();

    if (null == nomeFoto) {
        return new DefaultStreamedContent();
    }else{
        try {
            c.setTimeInMillis(Long.parseLong(dataMillis));
            RegistroRemote bean = LookupUtil.lookupRegistroBean();
            FotoDTO foto = bean.buscarFoto(nomeFoto, c.getTime());
            DefaultStreamedContent defaultStreamedContent = new DefaultStreamedContent(new ByteArrayInputStream(foto.getFoto()));
            return defaultStreamedContent;
        } catch (ExcecaoIntegracao e) {
            LOG.error(e.getMessage());
            Util.montaMensagem(FacesMessage.SEVERITY_ERROR, "Ocorreu um erro ao buscar a foto.", false);
        }
        return null;
    }
}

Images are normally displayed on <p:graphicImage> but when I click on the link generated by the tag <h:link value="#{imagemMB.foto}" target="_blank"> , a new tab opens in the browser showing a representation of the bytes , instead of the image. I understand that this happens because I am sending a stream to the browser tab and not an image, my intention is to open a new tab where the image can be viewed in full size.

Is there any way to "make the browser understand" that stream and display the image?

1 answer

0

Return a Streamedcontent with the following attributes. InputStream itStream = new ByteArrayInputStream(arrayDeBytes); StreamedContent file = new DefaultStreamedContent(itStream, extensaoDoArquivo, nomeArquivo); return file;

Browser other questions tagged

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