Buenas estoy tratando de mostrar imagenes de una bd para mostrarlas en un jsp pero a la hora de hacerlo eh llegado a esto en la pagina y ando perdido xD
en el dao tengo esto para recuperar los datos de la tabla en la bd en donde la imagen es un blob
public ArrayList<imagen> buscarEmAll() throws IOException {
ArrayList<imagen> lstGrupo = new ArrayList<>();
String sql = "select * from insumos";
try (PreparedStatement stmt = conexion.prepareStatement(sql)) {
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
imagen imagenos = new imagen();
imagenos.setnProducto(rs.getString("nombre_in"));
imagenos.setCategoria(rs.getString("categoria"));
imagenos.setDescripcion(rs.getString("descripcion"));
Blob blob = null;
blob = rs.getBlob("foto");
byte[] data = blob.getBytes(1, (int) blob.length());
BufferedImage img = null;
img = ImageIO.read(new ByteArrayInputStream(data));
imagenos.setFoto(img);
lstGrupo.add(imagenos);
}
}
} catch (SQLException ex) {
throw new RuntimeException("Error al buscar insumos sql", ex);
}
return lstGrupo;
}
mientras que en el servlet tendria esto:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); try (Connection cnx = ds.getConnection()) { insumosDAO dao = new insumosDAO(cnx); String strCategoria = request.getParameter("categoria"); ArrayList<imagen> lstGrupo = dao.buscarEmAll(); request.setAttribute("lstGrupo", lstGrupo); if (strCategoria != null && !strCategoria.isEmpty()) { ArrayList<imagen> lstGrupo2 = dao.buscarEmAll(); lstGrupo = lstGrupo2; request.setAttribute("lstGrupo", lstGrupo); } } catch (Exception ex) { request.setAttribute("mensajeError", "Oops!! " + ex.getMessage()); LOG.log(Level.SEVERE, "Error al consultar", ex); } finally { request.getRequestDispatcher("/listar.jsp").forward(request, response); } } para terminar en el jsp en donde con un foreach muestro los datos del arraylist del dao pero es en donde tengo ataos para mostrar la imagen que se ven al principio del post
<table>
<tr>
<th>Nombre</th>
<th>Categoria</th>
<th>Descripcion</th>
<th>Foto</th>
</tr>
<c:forEach var="imagen" items="${lstGrupo}">
<tr>
<td><c:out value="${imagen.nProducto}" /></td>
<td><c:out value="${imagen.categoria}" /></td>
<td><c:out value="${imagen.descripcion}" /></td>
<td><c:out value="${imagen.foto}" /></td>
<td> <img src="${imagen.foto}" /></td>
</tr>
</c:forEach>
</table>
<hr />
<a href="<c:url value="/" />">Volver a Opciones</a>
</body>
</html>
seria bkn que me dieran consejos en donde me equivoco como para darme una mejor idea del asunto, de todas maneras gracias PD: la clase en el dto la tengo asi public class imagen { private String nProducto; private String categoria; private String descripcion; private Image foto;