JSF open image (DefaultStreamedContent) in another screen

0
Hello, I'm working on a project with jsf and primefaces, where I have an entity named Record 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, load these images as byte [] and on-screen display 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>

ImageMB

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 in <p:graphicImage> but when I click the link generated by the <h:link value="#{imagemMB.foto}" target="_blank"> tag, a new tab opens in the browser showing a representation of the bytes, rather than the image. I understand that this happens because I'm 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 real size.

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

    
asked by anonymous 13.01.2018 / 20:24

1 answer

0

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

    
02.02.2018 / 18:51