se ti può servire ho fatto questo, è molto intuitivo:
codice:
public class FileDownload {
public static boolean download(URL fileToDownload, File location){
try {
URLConnection connection = fileToDownload.openConnection();
InputStream is = connection.getInputStream();
OutputStream os = new FileOutputStream(location);;
long start=new Date().getTime();
long nbytes=copy(is,os);
long finish=new Date().getTime();
System.out.println("Download completato: "+location.getAbsolutePath());
System.out.println(nbytes+" bytes in "+((finish-start)/1000)+" seconds");
return true;
} catch (IOException e) {
System.err.println("Download fallito: "+location.getAbsolutePath());
e.printStackTrace();
return false;
}
}
public static long copy(InputStream in, OutputStream out) throws IOException{
long bytesCopied = 0;
byte[] buffer = new byte[4096];
int bytes;
try{
while ( (bytes = in.read( buffer )) != -1 ){
out.write( buffer, 0, bytes );
bytesCopied += bytes;
}
}finally{
in.close();
out.close();
}
return bytesCopied;
}
}