get Remote File Size via URL - Java Network

Java examples for Network:URL Download

Description

get Remote File Size via URL

Demo Code


//package com.java2s;

import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    public static void main(String[] argv) throws Exception {
        String url = "java2s.com";
        System.out.println(getRemoteFileSize(url));
    }//from   w  w w  . j  a  va 2  s  .  co  m

    public static long getRemoteFileSize(String url) {
        long size = 0;
        try {
            HttpURLConnection conn = (HttpURLConnection) (new URL(url))
                    .openConnection();
            size = conn.getContentLength();
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }
}

Related Tutorials