Easy Tutorial
❮ Method Override Java Exceptions ❯

Java Example - Get Remote File Size

Java Example

The following example demonstrates how to get the size of a remote file:

Main.java File

import java.net.URL;
import java.net.URLConnection;

public class Main {
   public static void main(String[] args) throws Exception {
      int size;
      URL url = new URL("http://www.tutorialpro.org/wp-content/themes/tutorialpro/assets/img/newlogo.png");
      URLConnection conn = url.openConnection();
      size = conn.getContentLength();
      if (size < 0)
          System.out.println("Unable to get file size.");
      else
        System.out.println("File size is: " + size + " bytes");
      conn.getInputStream().close();
   }
}

The output of the above code is:

File size is: 4261 bytes

Java Example

❮ Method Override Java Exceptions ❯