Remote File Size

Remote File Size

How can I discover the size of a remote file stored on an HTTP server?

The HTTP protocol supports the transfer of extra information about thecontent referenced by a URL. This information is stored in the HTTPheader fields. The Content-Length header provides the size of theobject referenced by the URL. Not all web servers will provide thisinformation, so you should not develop your client code to rely on itsavailability.

The class provides access to any arbitrary HTTPheader field through the getHeaderField() method, which will returnthe value of header as a string. Two other methods,getHeaderFieldDate() and getHeaderFieldInt(), will return the valuesof fields containing dates or integers as Date and int typesrespectively. They are convenience methods that save you the troubleof parsing the header field. URLConnection also provides conveniencemethods for accessing commonly used header fields, such asLast-Modified, Content-Type, and, yes, even Content-Length. The valueof the Content-Length header is directly returned as an integer bygetContentLength(). If the header value does not exist, the methodreturns -1.

Before querying the value of a header, you first need to establish aconnection. The normal way to do this is to create a URL instancethat points to the object you want to access, and then create aURLConnection with openConnection(). The following exampledemonstrates how to open a URLConnection and obtain the byte length ofthe content referenced by the URL.

import*;import*;public final class URLFileSize {  public static final void main(String[] args) {    URL url;    URLConnection connection;    int fileSize;    if(args.length != 1) {      System.err.println("Usage: URLFileSize ");      return;    }    try {      url = new URL(args[0]);      connection = url.openConnection();      fileSize = connection.getContentLength();      if(fileSize < 0)	System.err.println("Could not determine file size.");      else	System.out.println(args[0] + "
Size: " + fileSize);      connection.getInputStream().close();    } catch(IOException e) {      e.printStackTrace();    }  }}
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as