Objeto ole

4 respostas
P

galera no meu bd (sql server) tem um campo que é uma foto (objeto ole),como eu faço para poder visualizar a imagen no jsp, ou seja fazer um select e mostrar a foto da pessoa ???

4 Respostas

R

Daí,

Podes fazer um servlet que recebe o conteúdo do banco e transforma numa figura usanod InputStream e umas classes da com.sun.imageio pra transformar no tipo que tu quiseres. Daí depois é só a página JSP ler chamar o servlet na hora que for montar o link da imagem.

Por exemplo:
<img src="/servlet/ImageReagerServlet?imageId=43" />

E o servlet pega o id que a JSP passou e recupera a imagem do banco e exibe.

public void doGet&#40;HttpServletRequest req, HttpServletResponse resp&#41; throws IOException &#123;
        // Get the absolute path of the image
        ServletContext sc = getServletContext&#40;&#41;;
        String filename = sc.getRealPath&#40;&quot;image.gif&quot;&#41;;
    
        // Get the MIME type of the image
        String mimeType = sc.getMimeType&#40;filename&#41;;
        if &#40;mimeType == null&#41; &#123;
            sc.log&#40;&quot;Could not get MIME type of &quot;+filename&#41;;
            resp.setStatus&#40;HttpServletResponse.SC_INTERNAL_SERVER_ERROR&#41;;
            return;
        &#125;
    
        // Set content type
        resp.setContentType&#40;mimeType&#41;;
    
        // Set content size
        File file = new File&#40;filename&#41;;
        resp.setContentLength&#40;&#40;int&#41;file.length&#40;&#41;&#41;;
    
        // Open the file and output streams
        FileInputStream in = new FileInputStream&#40;file&#41;;
        OutputStream out = resp.getOutputStream&#40;&#41;;
    
        // Copy the contents of the file to the output stream
        byte&#91;&#93; buf = new byte&#91;1024&#93;;
        int count = 0;
        while &#40;&#40;count = in.read&#40;buf&#41;&#41; &gt;= 0&#41; &#123;
            out.write&#40;buf, 0, count&#41;;
        &#125;
        in.close&#40;&#41;;
        out.close&#40;&#41;;
    &#125;

Achei esse código na net, mas é parecido com um que eu já fiz, então funciona, só que não to conseguindo achar a minha classe pra poder mostrar pra ti.

Basicamente o que tens que fazer é trocar a aprte que lê o arquivo do sistema e passar a ler do BD.

Agora, curiosidade, por que tu não armazenas só o endereço da imagem no banco e deixa a imagem no servidor mesmo ? Isso ia te agilizar muita coisa, sem falar que seria bem mais fácil.

[]s

P

eu ate faria isso, mas o bd nao é meu e ja tem mais de 10000 fotos no bd,porem a recuperação de uma unica foto fica bem mais leve,

valew,vou tentar implementar

P

tipo,to mais perdido q cego em tiroteio,a foto no bd ta nesse formato
0xFFD8FFE000104A46494600010100000100010000FFDB004300100B0C0E0C0A100E0D0E1211101318281A181616183123251D283A333D3C3933383740485C4E404457453738506D51575F626768673E4D71797064785C656763FFDB00431518AFF1A1A2F6342384263636363636363636363636363636363636363
por exemplo,eu queria fazer uma função para retornar a imagem ,no exemplo de cima eu teria que salvar a imagem em um lugar especifico, porem nao queria ter que salva-la e somente exibi-la no jsp…

algum caminho especifico para eu seguir ???

R

Mas foi o que eu falei, cara...

"Basicamente o que tens que fazer é trocar a aprte que lê o arquivo do sistema e passar a ler do BD."

Não sei como tás fazendo pra buscar do banco, mas encontrei um artigo legal na internet.

O grosso pra mostrar a imagem no servlet, é isso:
// Get the image Blob from the database
      Blob blob = rs.getBlob&#40; 1 &#41;;
      InputStream in = blob.getBinaryStream&#40;&#41;;

      // Output the blob to the HttpServletResponse
      res.setContentType&#40; &quot;image/jpeg&quot; &#41;;
      BufferedOutputStream out = new BufferedOutputStream&#40; res.getOutputStream&#40;&#41; &#41;;
      byte by&#91;&#93; = new byte&#91; 32768 &#93;;
      int index = in.read&#40; by, 0, 32768 &#41;;
      while &#40; index != -1 &#41;
      &#123;
        out.write&#40; by, 0, index &#41;;
        index = in.read&#40; by, 0, 32768 &#41;;
      &#125;
      out.flush&#40;&#41;;

Se quiseres o código fonte inteiro da galeria, tá nesse site: aqui

Ah, tá em inglês.

[]s

Criado 16 de agosto de 2006
Ultima resposta 31 de ago. de 2006
Respostas 4
Participantes 2