4.java获取网络资源的大小,根据资源的url下载资源
1.获取网络资源的大小
获取网络资源的大小其实就是根据url建立HTTP连接,然后发出请求得到响应后,从响应报文头中获取Content-Length 这个字段的值。
代码如下:
/**
* 给定url地址获取资源的大小(以字节为单位)
* @param urlStr
* @return
* @throws IOException
*/
public static long getResourceLength(String urlStr) throws IOException {
URL url=new URL(urlStr);
URLConnection urlConnection=url.openConnection();
urlConnection.connect();
//返回响应报文头字段Content-Length的值
return urlConnection.getContentLength();
}
测试:
public static void main

浙公网安备 33010602011771号