1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public static byte[] download(URL url) throws IOException {
URLConnection con = url.openConnection();
// On récupère la taille de l'élément cible
// (-1 si inconnu)
int length = con.getContentLength();
if (length<=0) {
length = 512;
}
InputStream is = con.getInputStream();
try {
ByteArrayOutputStream bao = new ByteArrayOutputStream(length);
byte[] buff = new byte[512];
int readed;
while ( (readed = is.read(buff, 0, 512))!=-1 ) {
bao.write(buff, 0, readed);
}
return bao.toByteArray();
} finally {
is.close();
}
} |