获取远程文件大小

import java.net.HttpURLConnection;
import java.net.URL;
        URL url = new URL("http://www.baidu.com/1.jpg");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        long bytesum = 0;
        long byteread = 0;
        byte[] buffer = new byte[1024];
        InputStream inputStream = con.getInputStream();
        while ((byteread = inputStream.read(buffer)) != -1) {
            bytesum += byteread;
        }
//bytesum得到的是byte,除以1024得到KB

你可能感兴趣的:(java)