URL url = new URL("https://example.com/file.pdf"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Get the length of the content in bytes int contentLength = connection.getContentLength(); System.out.println("Content length: " + contentLength);In this example, we create an HttpURLConnection to download a PDF file from a remote server. We use the getContentLength() method to get the size of the file in bytes before downloading it. Package Library: java.net package.