- /**
- * 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 @param imgPath
* @param @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);
}
本文提供了一个方法将字节单位转换为千字节和兆字节,包括实现步骤和示例代码。

4813

被折叠的 条评论
为什么被折叠?



