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

Asked

Viewed 950 times

1

I’m having trouble displaying images on a datagrid using both img and graphicImage passing to these tags the path of the database. 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);

}

In 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: imagens_manager test.png

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

Essa imagem mostra em modo debug quando tenta montar a inputStream

Exception Lançado:

GRAVE: Error in streaming dynamic resource. null
out 26, 2015 4:10:37 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path       [/sisgerenciador] threw exception
java.io.IOException: java.lang.NullPointerException
at org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:122)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

I also tried to use the virtual directory of Tomcat (C: Program Files Apache Software Foundation Tomcat 8.0 conf Catalina localhost img), but it does not work.

xml images.

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:\imagens_gerenciador"></Context> 
  • Could put the source code?

  • 1

    @rray Edited the post for better understanding.

  • Do you have <h:form> on your page? You need to use Streamedcontent?

  • @Rodrigo Tem <h:form>. It doesn’t have to be via Streamedcontent.

  • Your images are actually being created in the folders you set?

  • Thank you for answering, the solution I got is just below.

Show 1 more comment

1 answer

2

Solution was to create a Servlet and pass the image path as 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>
  • This solution is the same one I use to treat these cases, with some differences in implementation.

Browser other questions tagged

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