java 根据图片地址获取到图片的大小,单位kb或者Mb
/**
* byte(字节)根据长度转成kb(千字节)和mb(兆字节)
*
* @param bytes
* @return
*/
public static string bytes2kb(long bytes) {
bigdecimal filesize = new bigdecimal(bytes);
bigdecimal megabyte = new bigdecimal(1024 * 1024);
float returnvalue = filesize.divide(megabyte, 2, bigdecimal.round_up)
.floatvalue();
if (returnvalue > 1)
return (returnvalue + "mb");
bigdecimal kilobyte = new bigdecimal(1024);
returnvalue = filesize.divide(kilobyte, 2, bigdecimal.round_up)
.floatvalue();
return (returnvalue + "kb");
}
/**
* @title: pathsize
*@param imgpath
*@return 根据图片地址返回图片大小kb或者 mb
* @return string
* @throws
* @add (default no)
*/
public string pathsize(string imgpath) {
file file = new file(imgpath);
fileinputstream fis;
int filelen = 0;
try {
fis = new fileinputstream(file);
filelen = fis.available();
} catch (filenotfoundexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
return bytes2kb(filelen);
}
上一篇: GDAL2.2.4 C#中的编译及使用