1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public static void DownloadFile(String file) {
try {
URL racine = new URL(file);
getFile(racine);
} catch (MalformedURLException e) {
System.err.println(file + " : URL non comprise.");
} catch (IOException e) {
System.err.println(e);
}
}
private static void getFile(URL u) throws IOException {
URLConnection uc = u.openConnection();
String FileType = uc.getContentType();
int FileLenght = uc.getContentLength();
if (FileLenght == -1) {
throw new IOException("Fichier non valide.");
}
InputStream in = uc.getInputStream();
String FileName = u.getFile();
FileName = FileName.substring(FileName.lastIndexOf('/') + 1);
FileOutputStream WritenFile = new FileOutputStream(FileName);
byte[]buff = new byte[1024];
int l = in.read(buff);
while(l>0)
{
WritenFile.write(buff, 0, l);
l = in.read(buff);
}
WritenFile.flush();
WritenFile.close();
} |
Partager