Sample code for 30+ languages & platforms
Java

Get Size of File on Web Server

See more HTTP Examples

Demonstrates how to send a HEAD request to fetch information about a file on a web server, which hopefully includes the Content-Length in the response header.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http = new CkHttp();

    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpNoBody("HEAD","https://nodejs.org/dist/v10.16.1/node-v10.16.1-win-x64.zip",resp);
    if (success == false) {
        System.out.println(http.lastErrorText());
        return;
        }

    // Examine the response header.
    System.out.println(resp.header());

    // Here is a sample response header:

    // Date: Thu, 01 Aug 2019 12:47:27 GMT
    // Content-Type: application/zip
    // Content-Length: 17457922
    // Connection: keep-alive
    // Set-Cookie: __cfduid=da5b78dcdd8171683c5a578eb9c9f88c81564663647; expires=Fri, 31-Jul-20 12:47:27 GMT; path=/; domain=.nodejs.org; HttpOnly
    // Last-Modified: Wed, 31 Jul 2019 18:21:57 GMT
    // ETag: "5d41dc45-10a6302"
    // Accept-Ranges: bytes
    // Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    // Server: cloudflare
    // CF-RAY: 4ff7ebb32ac4e1e2-ORD

    // Get the Content-Length header.
    String contentLengthStr = resp.getHeaderField("Content-Length");
    // If the header exists...
    if (resp.get_LastMethodSuccess() == true) {
        System.out.println("Content-Length: " + contentLengthStr);
        // Your application can convert the contentLengthStr to an integer and then use it....
        }
  }
}