I'm trying to download a mp4 file from the web. I'd like to do it async and track the progress so it can be displayed in a progressbar.
My code looks as following:
URLConnection con = url.openConnection();
ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
Would creating a loop with transferFrom only reading up to 32KB each time and incrementing position be good practice, or is there a better way that would enable me to track the progress of the download? And how would I know when to stop transfering?
I just discovered that you can get the file size via the HTTP header field:
con.getHeaderFields().get("Content-Length").get(0)
Now knowing the filesize should make me able to implement the before mentioned loop.
Aucun commentaire:
Enregistrer un commentaire