根据网络文件的下载链接地址,获取文件的大小
直接上代码(记住要关闭相关的流)
/**
* 根据地址获得数据的字节流并转换成大小
* @param strUrl 网络连接地址
* @return
*/
public static String getFileSizeByUrl(String strUrl){
InputStream inStream=null;
ByteArrayOutputStream outStream=null;
String size="";
try {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection)();
("GET");
(5 * 1000);
inStream = ();
outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=(buffer)) != -1 ){
(buffer, 0, len);
}
byte[] bt = ();
if(null != bt && > 0){
DecimalFormat df = new DecimalFormat("#.00");
if ( < 1024) {
size = ((double) ) + "BT";
} else if ( < 1048576) {
size = ((double) / 1024) + "KB";
} else if ( < 1073741824) {
size = ((double) / 1048576) + "MB";
} else {
size = ((double) / 1073741824) +"GB";
}
("文件大小=:" + size);
}else{
("没有从该连接获得内容");
}
();
();
} catch (Exception e) {
();
}finally{
try{
if(inStream !=null){
();
}
if(outStream !=null){
();
}
} catch (IOException e) {
();
}
}
return size;
}