Upload de arquivo

3 respostas
S

E aí galera beleza?

TEnho que fazer upload em um site por jsp mas, numca trabalhei com upload. Alguém tem uma luz para eu poder começar?

Desde já agradeço!

3 Respostas

A
Cara, upload eu nao sei, mas download é assim:

public class Baixador {

/**A url de onde será feito o download (inclui o nome do arquivo)<em>/

private URL url;

/**O local onde o arquivo será gravado na máquina local (inclui o nome do arquivo)</em>/

private String destino;
/**
 * Cria uma instancia deste objeto
 * @param endNet Endereço de onde será baixado o arquivo (inclui o nome do arquivo)
 * @param destino O caminho da máquina local onde o arquivo será gravado (inclui o nome do arquivo)
 */
public Baixador( String endNet, String destino){
    try{
        url = new URL( endNet );
    }catch( MalformedURLException e ){
        System.out.println( "Erro: " + e.getMessage() );
    }
    this.destino = destino;
}

/**
 * Faz o download do arquivo para do local especificado 
 * em url para o local especificado em destino
 */
public void Baixar(){
    byte b[] = new byte[1];
    try{
        InputStream arq = url.openConnection().getInputStream();
        FileOutputStream arqNovo = new FileOutputStream( destino );
        int tamanho = arq.read(b, 0, 1);
        while( tamanho != -1){
            arqNovo.write( b, 0, tamanho );
            tamanho = arq.read(b, 0, 1);
        }
    }catch( Exception e ){
        System.out.println( e.getMessage() );
    }
}

}

Vê se o download te ajuda!

M

pra tua sorte eu fiz um upload exatamente hj no trabalho, eahehahae, ta ai cara

String contentType = request.getContentType&#40;&#41;;
    System.out.println&#40; &quot;Content type is &#58;&#58; &quot; + contentType &#41;;
    
    if &#40; &#40; contentType != null &#41; &amp;&amp; &#40; contentType.indexOf&#40; &quot;multipart/form-data&quot; &#41; &gt;= 0 &#41; &#41; 
    &#123;
        DataInputStream in = new DataInputStream&#40; request.getInputStream&#40;&#41; &#41;;
        int formDataLength = request.getContentLength&#40;&#41;;

        byte dataBytes&#91;&#93; = new byte&#91; formDataLength &#93;;
        int byteRead = 0;
        int totalBytesRead = 0;

        while &#40; totalBytesRead &lt; formDataLength &#41; 
        &#123;
            byteRead = in.read&#40; dataBytes, totalBytesRead, formDataLength &#41;;
            totalBytesRead += byteRead;
        &#125;


        String file = new String&#40; dataBytes &#41;;
        String saveFile = file.substring&#40; file.indexOf&#40; &quot;filename=\&quot;&quot; &#41; + 10 &#41;;
        saveFile = saveFile.substring&#40; 0, saveFile.indexOf&#40; &quot;\n&quot; &#41; &#41;;
        saveFile = saveFile.substring&#40; saveFile.lastIndexOf&#40; &quot;\\&quot; &#41; + 1, saveFile.indexOf&#40; &quot;\&quot;&quot; &#41; &#41;;

        //out.print&#40;dataBytes&#41;;

        int lastIndex = contentType.lastIndexOf&#40; &quot;=&quot; &#41;;
        String boundary = contentType.substring&#40; lastIndex + 1, contentType.length&#40;&#41; &#41;;

        //out.println&#40;boundary&#41;;

        int pos;

        pos = file.indexOf&#40; &quot;filename=\&quot;&quot; &#41;;

        pos = file.indexOf&#40; &quot;\n&quot;, pos &#41; + 1;

        pos = file.indexOf&#40; &quot;\n&quot;, pos &#41; + 1;

        pos = file.indexOf&#40; &quot;\n&quot;, pos &#41; + 1;

        int boundaryLocation = file.indexOf&#40; boundary, pos &#41; - 4;
        int startPos = &#40; &#40; file.substring&#40; 0, pos &#41; &#41;.getBytes&#40;&#41; &#41;.length;
        int endPos = &#40; &#40; file.substring&#40; 0, boundaryLocation &#41; &#41;.getBytes&#40;&#41; &#41;.length;

        FileOutputStream fileOut = new FileOutputStream&#40; &quot;teuDiretorio&quot; + &quot;\\&quot; + saveFile &#41;;

        //fileOut.write&#40;dataBytes&#41;;    
        fileOut.write&#40; dataBytes, startPos, &#40; endPos - startPos &#41; &#41;;

        fileOut.flush&#40;&#41;;
        fileOut.close&#40;&#41;;

        out.println&#40;&quot;File saved as &quot; + saveFile &#41;;
N

Hum… vou tentar.

Vc seta o contentType p/o arquivo que vc quer.
Transforma o seu arquivo em um array de bytes.
E escreve atraves da classe ServletOutputStream, que vc pode pegar atraves de response.getOutputStream().

Bem, nunca fiz. Só tinha feito pelo struts. :oops:

Criado 6 de agosto de 2004
Ultima resposta 6 de ago. de 2004
Respostas 3
Participantes 4