Java 实例 – 获取远程文件大小
最后更新于:2022-03-27 01:06:39
Java 实例 – 获取远程文件大小
以下实例演示了如何获取远程文件的大小:
Main.java 文件
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://docs.gechiui.com/w3school/wp-content/themes/runoob/assets/img/newlogo.png");
URLConnection conn = url.openConnection();
size = conn.getContentLength();
if (size < 0)
System.out.println("无法获取文件大小。");
else
System.out.println("文件大小为:" + size + " bytes");
conn.getInputStream().close();
}
}
import java.net.URLConnection; public class Main {
public static void main(String[] args) throws Exception {
int size;
URL url = new URL("http://docs.gechiui.com/w3school/wp-content/themes/runoob/assets/img/newlogo.png");
URLConnection conn = url.openConnection();
size = conn.getContentLength();
if (size < 0)
System.out.println("无法获取文件大小。");
else
System.out.println("文件大小为:" + size + " bytes");
conn.getInputStream().close();
}
}
以上代码运行输出结果为:
文件大小为:4261 bytes