Java Examples - Get Remote File Size

Java 实例Java Examples

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.example.com/wp-content/themes/example/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:

文件大小为:4261 bytes

Java 实例Java Examples

Other extensions