Tenho um campo longblob salvo num banco de dados mysql, que se assemelha a algo como “com.mysql.cj.jdbc.Blob@1b7ec72f”. Quero trazer esse campo e representá-lo como imagem dentro de uma tag img, na aplicação java EE, com servlets, JSP e JDBC que estou construindo. Pesquisei bastante mas ainda não entendi como posso fazer tal coisa. Se alguém tiver um exemplo que possa me ajudar, agradeço.
Deixo alguns trechos do código que estou usando:
Método da classe DAO, que pega o valor blob da coluna “image”:
public ArrayList<Destiny> getAllDestinys() {
String sql = "SELECT * FROM destiny";
ArrayList<Destiny> arrDestinys = new ArrayList<Destiny>();
Blob blob = null;
try {
PreparedStatement pst = ConnectionBd.getConnection().prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Destiny destiny = new Destiny();
blob = rs.getBlob("image");
destiny.setIdDestiny(rs.getInt("idDestiny"));
destiny.setName(rs.getString("nameDestiny"));
destiny.setImage(blob.getBytes(1, (int) blob.length()));
destiny.setCity(rs.getString("city"));
arrDestinys.add(destiny);
}
pst.close();
rs.close();
} catch (SQLException e) {
System.out.println("Erro: "+e);
}
return arrDestinys;
}
Classe ReadDestinyController, que pega o resultado do método anterior e manda para a ver.jsp:
package controllers;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import agency.vacation.good.daos.DestinyDao;
@WebServlet("/ReadDestinyController")
public class ReadDestinyController extends HttpServlet {
private static final long serialVersionUID = 1L;
private DestinyDao destinyDao = new DestinyDao();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("arrDestinys", this.destinyDao.getAllDestinys());
RequestDispatcher rd = request.getRequestDispatcher("ver.jsp");
rd.forward(request, response);
}
}
A ver.jsp com uma tabela simples apenas para visualizar o resultado:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Login</th>
<th>Senha</th>
</tr>
</thead>
<tbody>
<c:forEach items="${arrDestinys}" var="arrDestiny">
<tr>
<td>${arrDestiny.idDestiny}</td>
<td>${arrDestiny.name}</td>
<td>${arrDestiny.city}</td>
<td>${arrDestiny.image}</td>
<td><img src="${arrDestiny.image}" width="400px"/></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
Reparem que eu até arrisquei colocar a saída do blob na tag img, mas provavelmente da maneira errada. Os outros dados que são números ou textos aparecem normalmente.