/**
* 获取本地图片的字节数
* @param imgPath
* @return
*/
public static 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);
}
/**
* 将获取到的字节数转换为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");
}
public static void main(String[] args) {
String imgUrl="E:\\vacations.ctrip.com_852377.4201748744.jpg";
String pathSize = pathSize(imgUrl);
System.out.println("获取到图片的大小: " + pathSize);
}
计算图片的大小
最新推荐文章于 2024-05-21 09:17:19 发布