Mapeamento de campos Blob usando struts

0 respostas
cubas

Pessoal. Criei um servlet para ler uma imagem BLOB

import java.sql.Blob;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.cubas.astral.hibernate.HibernateFactory;
import br.cubas.astral.teste.Teste;

public class ShowImageServlet extends HttpServlet {

    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("ISO8859-1");

        Teste teste = new Teste();
        String id = request.getParameter("id");
        org.hibernate.Session hibernateSession = HibernateFactory.getSessionFactory();

        try {
            teste = (Teste) hibernateSession.load(Teste.class, Integer.parseInt(id));
            if (!teste.getMyfile().equals(null)) {
                Blob photo = teste.getMyfile();
                InputStream in = photo.getBinaryStream();
                OutputStream out = response.getOutputStream();
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) != -1) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            } else {
                System.out.println("NAO FOI");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Fiz o mapeamento com o nome de /ImagemPagina

Na minha página jsp uso o seguinte código chamando este servlet dentro de uma tag src

<td width="32%" bgcolor="#FFFFFF">
    <img src= ImagemPagina?id=1 width="250" height="183" border="0">
</tr>

É possível fazer isto utilizando struts ?

Obrigado
Cubas

Criado 9 de setembro de 2008
Respostas 0
Participantes 1