UpLoad em Jps/Servlets?

5 respostas
G

:?: Alguem tem um exemplo de como fazer um pagina para upload???
:!: CASO: Em um de vendas de imoveis, o usuario ou administrador poderá enviar uma foto do imovel … Como fazer…
-> Fazer um upload da foto para uma pasta e pegar o link e gravar no BD? Ha possibilidade de poder gravar a foto (gif,jpeg) direto no MySql???

… Onde encontro um site de hospedagem bom e barato???

5 Respostas

V

Use a Commons FileUpload do Projeto Apache/Jakarta.

E para gravar uma imagem num campo de tabela do MySQL, este campo deve ter o tipo Blob

M

site bom e barato eu não sei... mas ai vai um exemplo bem tosco de upload de arquivo pro servidor ehhehehe, mas funciona

String contentType = request.getContentType();
    System.out.println( "Content type is :: " + contentType );
    
    if ( ( contentType != null ) && ( contentType.indexOf( "multipart/form-data" ) >= 0 ) ) 
    {
        DataInputStream in = new DataInputStream( request.getInputStream() );
        int formDataLength = request.getContentLength();

        byte dataBytes[] = new byte[ formDataLength ];
        int byteRead = 0;
        int totalBytesRead = 0;

        while ( totalBytesRead < formDataLength ) 
        {
            byteRead = in.read( dataBytes, totalBytesRead, formDataLength );
            totalBytesRead += byteRead;
        }

        String file = new String( dataBytes );
        String saveFile = file.substring( file.indexOf( "filename=\"" ) + 10 );
        saveFile = saveFile.substring( 0, saveFile.indexOf( "\n" ) );
        saveFile = saveFile.substring( saveFile.lastIndexOf( "\\" ) + 1, saveFile.indexOf( "\"" ) );

        //out.print(dataBytes);

        int lastIndex = contentType.lastIndexOf( "=" );
        String boundary = contentType.substring( lastIndex + 1, contentType.length() );

        //out.println(boundary);

        int pos;

        pos = file.indexOf( "filename=\"" );

        pos = file.indexOf( "\n", pos ) + 1;

        pos = file.indexOf( "\n", pos ) + 1;

        pos = file.indexOf( "\n", pos ) + 1;

        int boundaryLocation = file.indexOf( boundary, pos ) - 4;
        int startPos = ( ( file.substring( 0, pos ) ).getBytes() ).length;
        int endPos = ( ( file.substring( 0, boundaryLocation ) ).getBytes() ).length;

        FileOutputStream fileOut = new FileOutputStream( "C:\\" + saveFile );

        fileOut.write( dataBytes, startPos, ( endPos - startPos ) );

        fileOut.flush();
        fileOut.close();

        out.println("File saved as " + saveFile );
G

Blz…
… Onde encontro um site de hospedagem JSP/Struts???

M

Salve Gabriel!!!

Cara tem alguns bons o HostSul, o LocalWeb, o mesmo o Uai, eu utilizo o servido do uai muito bom, atende as minhas necessidades beleza! www.uai.com.br/assine vê o que melhor lhe atende…

até mais!

C

cara estou testando esse seu código para ver se entendo como isso funciona mas quando eu especifico o local ele dá permissão negada, creio eu que seja a permissão de minhas pastas para gravar e eetc, mais já abilitei TUDO nessa pasta e nada como devo fazer ???

FileOutputStream fileOut = new FileOutputStream( “\webapps\ROOT\imagens\acervo\” + saveFile ); e isso tá certo tem que ter dois “\”

me juda

Criado 10 de setembro de 2004
Ultima resposta 28 de nov. de 2004
Respostas 5
Participantes 5