个人收藏小代码:
public static String getFileSize(String path) {
File ff = new File(path);
String tmplen = "0.0";
if (ff.exists()) {
long len = ff.length();
tmplen = (new StringBuffer(String.valueOf(len / 1024L))).toString();
}
else {
tmplen = getRemoteFileSize(path);
}
return tmplen;
}
public static String getRemoteFileSize(String path) {
String tmplen = "1.0";
try {
URL url = new URL(path);
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
if (httpConnection.getResponseCode() == 200) {
long filelen = httpConnection.getContentLength();
tmplen = (new StringBuffer(String.valueOf(filelen / 1024L))).toString();
}
}
catch (UnknownHostException unknow) {
logger.error("GetRemoteFileSize error1:" + unknow);
//unknow.printStackTrace();
}
catch (IOException ioe) {
logger.error("GetRemoteFileSize error2:" + ioe);
//ioe.printStackTrace();
}
catch (Exception e) {
logger.error("GetRemoteFileSize error3:" + e);
//e.printStackTrace();
}
return tmplen;
}