How to know the size of a file before downloading it?

you can get a header called Content-Length form the HTTP Response object that you get, this will give you the length of the file.
you should note though, that some servers don’t return that information, and the only way to know the actual size is to read everything from the response.

Example:

URL url = new URL("http://server.com/file.mp3");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();

Leave a Comment