Skip to content

Java获取网络资源文件大小(单位:字节)

java
public static void main(String[] args) throws Exception {
    long fileSize = 0L;
    URL url = new URL("http://xxx.apk");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("HEAD");
    connection.connect();
    int responseCode = connection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        String contentLength = connection.getHeaderField("Content-Length");
        if (contentLength != null) {
            fileSize = Long.parseLong(contentLength);
        }
    }
    System.out.println(fileSize);
}

MIT版权,未经许可禁止任何形式的转载