How to display a list of images with path saved in the JSF2.2 + Primefaces 4 database

1

I'm having difficulty displaying images in a dataGrid using both the img and graphicImage passing the tags to the database path. Could someone help?

I'm using tomcat 8.

The last way I tried was this:

No Bean:

public StreamedContent getImage(Noticia noticia) {
  File arquivo = new File(noticia.getImagem()); // caminho do banco de dados
  FileInputStream inputStream = null;
  try {
    if(arquivo.exists()){
       inputStream = new FileInputStream(arquivo);
    }else{
       return new DefaultStreamedContent();
    }
  } catch (Exception e) {
     e.printStackTrace();
  }
return new DefaultStreamedContent(inputStream);

}

No xhtml:

<p:dataGrid id="noticias" var="noticia" value="#{noticiaBean.listaTesteNoticia}" paginator="true" rows="20" emptyMessage="Não existem notícias.">
  <f:facet name="header">Lista de Notícias</f:facet>
  <p:panel header="Notícia">
     <p:column>
        <p:graphicImage value="#{noticia.imagemMontada}" cache="false" /> <!-- objeto do tipo StreamedContent-->
     </p:column>
  </p:panel>
</p:dataGrid>

In the database you have this path: c: \ screenshots \ test.png

This image shows in debug mode when trying to mount the inputStream:

ExceptionReleased:

GRAVE:Errorinstreamingdynamicresource.nullout26,20154:10:37PMorg.apache.catalina.core.StandardWrapperValveinvokeGRAVE:Servlet.service()forservlet[FacesServlet]incontextwithpath[/sisgerenciador]threwexceptionjava.io.IOException:java.lang.NullPointerExceptionatorg.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:122)atjavax.faces.webapp.FacesServlet.service(FacesServlet.java:643)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atorg.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)atorg.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)atorg.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)atorg.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)atorg.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)atorg.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)atorg.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)atorg.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)atjava.lang.Thread.run(Thread.java:745)

Ialsotriedtousethetomcatvirtualdirectory(C:\ProgramFiles\ApacheSoftwareFoundation\Tomcat8.0\conf\Catalina\localhost\img),butitdoesnotwork.

images.xml

<?xmlversion="1.0" encoding="UTF-8"?>
<Context docBase="C:\imagens_gerenciador"></Context> 
    
asked by anonymous 26.10.2015 / 17:47

1 answer

2

Solution was to create a servlet and pass the path of the image as a parameter:

Servlet:

@WebServlet("/image")    
public class ImagemServlet extends HttpServlet {

 private static final long serialVersionUID = 1460571643688705941L;


    private String imagePath;


    public void init() throws ServletException {


        this.imagePath = "c:/diretorioDesejado/";


    }

    // Actions ------------------------------------------------------------------------------------

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String requestedImage = request.getParameter("imagem");


        File image = new File(imagePath, requestedImage);

        String contentType = getServletContext().getMimeType(image.getName());

        response.reset();
        response.setContentType(contentType);
        response.setHeader("Content-Length", String.valueOf(image.length()));

        Files.copy(image.toPath(), response.getOutputStream());
    }

}

XHTML:

<p:dataGrid id="id" var="variavel" value="#{bean.lista}">
    <p:graphicImage value="/image?imagem=#{variavel.caminhoDaImagem}"/>
</p:dataGrid>
    
28.10.2015 / 13:36