Salvar e recuperar imagens do banco de dados usando jsp

4 respostas
F

Galera tô precisando de uma ajuda. Um trabalho para o curso: Preciso salvar uma foto e depois recuperá-la. Salvar e recuperar o caminho da foto, eu consegui fazer. O desafio está sendo em recuperar a imagem do banco. Usando página “jsp”.
Segue os exemplos:

Para salvar (está funcionando) na jsp usei <input type="file"/>, com o seguinte método:

public void inserir (Aluno alu) throws Exception{
        String sql = "INSERT INTO tb_aluno (nome, foto, sexo, estado_civil, cidade, uf, cpf, descricao) VALUES (?,?,?,?,?,?,?,?)";
        try{
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, alu.getNome());
            File image = new File(alu.getFoto());
            FileInputStream fis = new FileInputStream(image);
            stmt.setBinaryStream(2, (InputStream)fis,(int)(image.length()));
            stmt.setInt(3, alu.getSexo());
            stmt.setInt(4, alu.getEstado_civil());
            stmt.setString(5, alu.getCidade());
            stmt.setInt(6, alu.getUf());
            stmt.setString(7, alu.getCpf());
            stmt.setString(8, alu.getDescricao());
            stmt.execute();
            stmt.close();
        }catch(SQLException erro){
            throw new RuntimeException(erro);
        }
    }

Como faço para recuperar a imagem do banco usando a seguinte ArrayList?
Quero mostrar numa <img src="" width="40" height="50">
Método:

public ArrayList<Aluno> listar_por_nome (String value)throws Exception{
        String sql = "SELECT * FROM tb_aluno WHERE nome LIKE '%"+value+"%'";
        try{
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            while(rs.next()){
                Aluno alu = new Aluno();
                alu.setId(rs.getInt("id"));
                alu.setNome(rs.getString("nome"));
                alu.setFoto(rs.getString("foto"));
                alu.setSexo(rs.getInt("sexo"));
                alu.setEstado_civil(rs.getInt("estado_civil"));
                alu.setCidade(rs.getString("cidade"));
                alu.setUf(rs.getInt("uf"));
                alu.setCpf(rs.getString("cpf"));
                alu.setDescricao(rs.getString("descricao"));
                lista.add(alu);
            }
        }catch(SQLException erro){
            throw new RuntimeException(erro);
        }
        return lista;
    }

É claro que o exemplo acima só traz um monte de caracateres!

No aguardo gente.

4 Respostas

F

O pessoal, ajuda aê!

T

vc ta usando scriptlet no jsp, jstl, jsf ?

F

JSP cru!

T

ve se esse link t ajuda

a ideia é essa

InputStream input = resultSet.getBinaryStream("coluna_imagem");

if(input != null){

   ByteArrayOutputStream output = new ByteArrayOutputStream();
   // set read buffer size

   byte[] rb = new byte[1024];
   int ch = 0;

   while ((ch = input.read(rb)) != -1){	
       output.write(rb, 0, ch);
   }

   // transfer to byte buffer
   byte[] b = output.toByteArray();
   input.close();
   output.close();

   // onde o método setImagem espera um array de bytes
   umObjeto.setImagem(b);                     
}
Criado 18 de novembro de 2011
Ultima resposta 22 de nov. de 2011
Respostas 4
Participantes 2