Selected Reading

Java - URLConnection getContentLengthLong() Method



Description

The Java URLConnection getContentLengthLong() method returns the value of the content-length header field as a long.

Declaration

Following is the declaration for java.net.URLConnection.getContentLengthLong() method

public int getContentLengthLong()

Parameters

NA

Return Value

the content length of the resource that this connection's URL references, or -1 if the content length is not known.

Exception

NA

Example 1

The following example shows the usage of Java URLConnection getContentLengthLong() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getContentLengthLong(), we're getting the value of content-length header field of URLConnection instance and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.getContentLengthLong());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

-1

Example 2

The following example shows the usage of Java URLConnection getContentLengthLong() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getContentLengthLong(), we're getting the value of content-length header field of URLConnection instance and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.getContentLengthLong());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

-1

Example 3

The following example shows the usage of Java URLConnection getContentLengthLong() method for a url of google with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getContentLengthLong(), we're getting the value of content-length header field of URLConnection instance and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.getContentLengthLong());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

-1
java_urlconnection.htm
Advertisements