• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to monitor a progress of file download?

 
Andreas Hollmann
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

Do you know how to monitor the progress of a file-download?
For example if I download a file in Firefox I see the progress.

Best Regards
Andreas
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just write some code which keeps track of how many bytes have been read from the InputStream you're downloading from.

If you wanted to be fancy you could write an InputStream to wrap around that one and count the bytes as they go by. Or if you suspected that somebody else had already had that idea, you could poke around in the API documentation for a while until you found ProgressMonitorInputStream.
 
Andreas Hollmann
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for fast reply,

I used already Streams to download files, but now I want to present the download progress of downloaded file as progress-bar (o% to 100%).

if I would know the size of downloaded file I could divide received bytes from InputStream by the file-size and get my progress in percent.

Do you know how I can find out the file-size of the file which should be downloaded?

Best Regards
Andreas
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Hollmann wrote:Do you know how I can find out the file-size of the file which should be downloaded?


Ah well, that's an entirely different question, which is nothing to do with monitoring the download. The answer depends on how you're downloading the file. If it's via HTTP you might have a header which is named Content-Length (or something like that, I'm posting from memory). If you don't have that header then you won't know the size of the file. If it's via FTP, there's usually a command where you can ask for that sort of information before you start downloading it. If you're using some other method, then it's up to you to research that method and see what it can do for you.
 
Andreas Hollmann
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's via HTTP you might have a header which is named Content-Length (or something like that, I'm posting from memory)

Yes I supposed some thing like that. Is there any java-tools for download of files over HTTP without parsing the HTTP header?

Best Regards
Andreas
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't expect so, since extracting the headers is such a simple thing to do.

Or to put it another way, of course there is, it seems like you already have code which downloads the file without looking at the headers.
 
Andreas Hollmann
Greenhorn
Posts: 27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for advices,
Here I found an example how header can be extracted. As you sad it is simple:


Thanks again
Andreas
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that the returned content length can be -1 if it's not available.
 
Andreas Hollmann
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
int contentLength = connection.getContentLength();


Event simpler :-)
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Keep in mind that the returned content length can be -1 if it's not available.

Yes, and I think putting even:

would be appropriate condition to display an error to the user.

One more suggestion, if I may: Make sure the class responsible for monitoring download implements Runnable, so you can use it to download multiple files simultaneously.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!