Size of file on the Internet using Java

Determining the size of a file on the Internet seems a little tricky, but it is a quite simple and easy task. Java provides some built-in features that can be used for the given task. In this article, we will discuss how to make a connection to the Internet and fetch the size of a given file.

How to establish a Connection on Internet using Java


The modern Internet is all about World Wide Web. Tim Berners-Lee invented a way to locate all the resources on Web and he named it Uniform Resource Locator. It provides the feature to identify resources distinctly over the Internet. There are four components of a URL −

  • Protocol

  • Host name or IP address

  • Port number

  • Path

The URL class of Java networking is used to make connections or to locate resources across the Internet with the means of URLs. This class throws MalformedURLException.


URL nameOfObject = new URL( “URLspecifier” );


URLspecifier is the actual URL or we can say link of the resource. It must be enclosed in double quotes.

To access content information of a URL, we need to define an object of ‘URLConnection’ class using the inbuilt method of ‘URL’ class ‘openConnection()’.


It is a class that is used to access the information of a specified resource, i.e. URL on the Internet. First, it examines the properties of the URL before retrieving it. To retrieve the information regarding the size of a specified resource, it defines a method named ‘getContentLengthLong()’. It is used with the object of ‘URLConnection’ class.

Java Program to Check Size of a File on Internet

Steps need to follow

  • First, define an object of the URL class along with the link of given file.

  • Create an object of class ‘URLConnection’ using object of ‘URL’ class.

  • Using ‘getContentLengthLong()’ retrieve the file size and store it in a variable named ‘fileSize’ of type long.

  • Now, we divide the received file size by 1024 to get the size of the file in KiloBytes.

  • In the end, with the help of if-else block print the result.


public class UCDemo {
   public static void main(String args[]) throws Exception {   
      try {
         // link of file   
         URL shareLink = new URL("");
         // making connection with the file
         URLConnection urlConn = shareLink.openConnection(); 
         // retrieving type of the file
         System.out.println("Type of the content: " + urlConn.getContentType());
         // retrieving size of the file
         long fileSize = urlConn.getContentLengthLong();
         long kb = fileSize / 1024;
         if(fileSize == -1) {
            System.out.println("Cannot determine the Size of file!!");
         } else {
            System.out.println("Size of the given file in KB: " + kb + " KB");
      catch(Exception exp) {
         System.out.println("Something went wrong!! Please check the file type!!");


Type of the content: application/pdf
Size of the given file in KB: 72 KB


An IP address is either a 32-bit or 128-bit unsigned number that uniquely identifies devices on the Internet. It is easy to remember the name of IP host rather than the numerical address. Therefore, the URLs are found in the form of strings. In this article, we have learned about URL and URLConnection class that helps in making a connection with World Wide Web. Both the classes are available in ‘’ package.

Updated on: 15-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started