Created
August 9, 2017 14:02
-
-
Save samuelsonbrito/53533a88df5dcf3ddcab4e14de0d3cb4 to your computer and use it in GitHub Desktop.
Download via java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static File gravaArquivoDeURL(String stringUrl, String pathLocal) { | |
try { | |
//Encapsula a URL num objeto java.net.URL | |
URL url = new URL(stringUrl); | |
//Queremos o arquivo local com o mesmo nome descrito na URL | |
//Lembrando que o URL.getPath() ira retornar a estrutura | |
//completa de diretorios e voce deve tratar esta String | |
//caso nao deseje preservar esta estrutura no seu disco local. | |
String nomeArquivoLocal = url.getPath(); | |
//Cria streams de leitura (este metodo ja faz a conexao)... | |
InputStream is = url.openStream(); | |
//... e de escrita. | |
FileOutputStream fos = new FileOutputStream(pathLocal+nomeArquivoLocal); | |
//Le e grava byte a byte. Voce pode (e deve) usar buffers para | |
//melhor performance (BufferedReader). | |
int umByte = 0; | |
while ((umByte = is.read()) != -1){ | |
fos.write(umByte); | |
} | |
//Nao se esqueca de sempre fechar as streams apos seu uso! | |
is.close(); | |
fos.close(); | |
//apos criar o arquivo fisico, retorna referencia para o mesmo | |
return new File(pathLocal+nomeArquivoLocal); | |
} catch (Exception e) { | |
//Lembre-se de tratar bem suas excecoes, ou elas tambem lhe tratarão mal! | |
//Aqui so vamos mostrar o stack no stderr. | |
e.printStackTrace(); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment