Download de arquivo por HTTP

2 respostas
S

Como eu posso fazer o download de um arquivo contido em uma URL ex: “http://meudominio.com/meuarquivo.txt

Obrigado.

2 Respostas

sunshine

use o httpclient da apache

porfirio

Ou pode sempre ir pela maneira dificil…

http://www.hr.utah.edu/blog/?p=4

public class JavaNetUrlExample {
    public static void main(String[] args){
        String webFile = "http://www.hr.utah.edu/";
        try {
            URL url = new URL(webFile);
            InputStream in = url.openStream();
            byte[] buf = new byte[1024];
            int len;
            while( (len = in.read(buf)) &gt 0 ){
                for(int i = 0; i &lt len; i++){
                    System.out.print((char)buf[i]);
                }
            }
            //close the stream
            in.close();
        }catch (MalformedURLException e){
            //TODO
        }catch (IOException e){
            //TODO:FileNotFoundException caught here
        }finally{
            //TODO
        }
    }
}
Criado 26 de maio de 2007
Ultima resposta 26 de mai. de 2007
Respostas 2
Participantes 3