Galeria de fotos com Servlet

3 respostas
L

Olá Pessoal,

estou tentando fazer uma galeria de fotos com um servlet. Já consegui fazer o upload da foto e salvar no servidor. Agora estou precisando resolver como exibir essa imagem em um jsp. O servlet salva a imagem no servidor mas não em uma pasta que eu possa navegar.

Alguém pode me dar uma dica ou sabe como posso resolver esse problema?

[]'s
Leo

3 Respostas

C

Oi Leo,

você tem que recuperar a imagem do servidor e jogá-la no browser do cliente.

J

Dae cara.. eu fiz algo parecido.... tu cria um servlet e na tag img src tu passar esse servlet com o id da tua imagem.. é mais ou menos isso:

public class ServLoadImage extends HttpServlet {
    
        
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String arquivo;
        try {            
            arquivo = request.getParameter("id");
            File file = new File(arquivo);
            this.download( file.getName(), read( file ), response );
            
        } catch( IOException e ) {
            try {
                throw new IOException(e.getMessage());
            } catch (IOException ex) {
                ex.printStackTrace();
                throw new ServletException(e.getMessage() + " ::  Erro ao carregar arquivo");
            }
        }    
    }    
    
    // Passe para esse método um File com o arquivo desejado.
    public void download( File file, HttpServletResponse response )
    throws IOException {
        this.download( file.getName(), read( file ), response );
    }
    
    //  o arquivo no servidor
    public byte[] read( File file ) throws IOException {
        
        byte[] content = null;
        int fileLength = (int) file.length();
        FileInputStream fileInput = null;
        
        try {
            fileInput = new FileInputStream(file);
            
            BufferedInputStream bufferedInput = new BufferedInputStream(
                    fileInput);
            content = new byte[fileLength];
            bufferedInput.read(content, 0, fileLength);
            bufferedInput.close();
        } finally {
            if (fileInput != null) {
                fileInput.close();
            }
        }
        
        return content;
    }
    
    // Faz o download do arquivo
    public void download( String filename, byte[] content,
            HttpServletResponse response ) throws IOException {
        
        response.setContentType( "image/jpeg" );
        
        ServletOutputStream outStream = response.getOutputStream();        
        // envia o conteúdo do arquivo para o stream de resposta
        try {
            outStream.write( content );
            outStream.flush();
        } finally {
            outStream.close();
        }
    }
    
...

e no jsp tu faz isso:

<img src="ServLoadImage?id=<%=tipo.getArquivoPBancoImagem()%>" />

Acho que pode te ajudar..
Abraço e fica com Deus!!

E

kra da pra colocar o codigo completo da .jsp queria saber de onde vem esse tipo do codigo abaixo

<%=tipo.getArquivoPBancoImagem()

Esqueleto

Criado 1 de dezembro de 2007
Ultima resposta 11 de dez. de 2007
Respostas 3
Participantes 4