I'm working on a project for school, and I'm implementing a tool which can be used to download files from the web ( with a throttling option ). The thing is, I'm gonna have a GUI for it, and I will be using a JProgressBar
widget, which I would like to show the current progress of the download. For that I would need to know the size of the file. How do you get the size of the file prior to downloading the file.
Java URLConnection : how can I find out the size of a web file
httphttp-headersjava
Related Topic
- Java – How to create a Java string from the contents of a file
- Java – How to break out of nested loops in Java
- Java – How to get the current stack trace in Java
- Javascript – Detect when browser receives file download
- Java – Download a file with Android, and showing the progress in a ProgressDialog
- Java – How to avoid Java code in JSP files, using JSP 2
- Java – How to create a memory leak in Java
Best Answer
Any HTTP response is supposed to contain a Content-Length header, so you could query the URLConnection object for this value.
It might not always be possible for a server to return an accurate Content-Length, so the value could be inaccurate, but at least you would get some usable value most of the time.
update: Or, now that I look at the URLConnection javadoc more completely, you could just use the getContentLength() method.