java 如何把byte轉(zhuǎn)化為KB、MB、GB的方法
java把byte轉(zhuǎn)化為KB、MB、GB的方法
從服務(wù)器下載一個(gè)文件時(shí)候,往往告訴你的是相應(yīng)的KB,MB,GB。
方法如下
public static String getNetFileSizeDescription(long size) {
StringBuffer bytes = new StringBuffer();
DecimalFormat format = new DecimalFormat("###.0");
if (size >= 1024 * 1024 * 1024) {
double i = (size / (1024.0 * 1024.0 * 1024.0));
bytes.append(format.format(i)).append("GB");
}
else if (size >= 1024 * 1024) {
double i = (size / (1024.0 * 1024.0));
bytes.append(format.format(i)).append("MB");
}
else if (size >= 1024) {
double i = (size / (1024.0));
bytes.append(format.format(i)).append("KB");
}
else if (size < 1024) {
if (size <= 0) {
bytes.append("0B");
}
else {
bytes.append((int) size).append("B");
}
}
return bytes.toString();
}這里用到了這個(gè)類:
DecimalFormat
java字節(jié)轉(zhuǎn)換成MB,GB,TB和MB,GB,TB轉(zhuǎn)成字節(jié)的工具類
import java.math.BigDecimal;
public class ByteUtil {
public static final Integer KB_SIZE = 2 << 9;
public static final Integer MB_SIZE = 2 << 19;
public static final Integer GB_SIZE = 2 << 29;
public static BigDecimal bytes2Unit(long bytes, Integer unit) {
BigDecimal size = new BigDecimal(bytes);
BigDecimal u = new BigDecimal(unit);
return size.divide(u, 2, BigDecimal.ROUND_DOWN);
}
public static Long unit2Byte(BigDecimal decimal,Integer unit) {
return decimal.multiply(BigDecimal.valueOf(unit)).longValue();
}
public static Long kb2Byte(BigDecimal decimal) {
return decimal.multiply(BigDecimal.valueOf(KB_SIZE)).longValue();
}
public static Long mb2Byte(BigDecimal decimal) {
return decimal.multiply(BigDecimal.valueOf(MB_SIZE)).longValue();
}
public static Long gb2Byte(BigDecimal decimal) {
return decimal.multiply(BigDecimal.valueOf(GB_SIZE)).longValue();
}
public static BigDecimal bytes2Kb(long bytes) {
return bytes2Unit(bytes,KB_SIZE);
}
public static BigDecimal bytes2Mb(long bytes) {
return bytes2Unit(bytes,MB_SIZE);
}
public static BigDecimal bytes2Gb(long bytes) {
return bytes2Unit(bytes,GB_SIZE);
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決SpringCloud Gateway采用OpenFeign遠(yuǎn)程調(diào)用失敗的問題
在使用SpringCloud網(wǎng)關(guān)進(jìn)行統(tǒng)一鑒權(quán)和認(rèn)證過程中,通過OpenFeign遠(yuǎn)程調(diào)用鑒權(quán)服務(wù)器接口時(shí)可能會(huì)遇到遠(yuǎn)程調(diào)用失敗的問題,這通常是因?yàn)镠ttpMessageConverters沒有被正確注入到Spring容器中2024-09-09
SpringBoot整合Mybatis與MybatisPlus方法詳細(xì)講解
這篇文章主要介紹了SpringBoot整合Mybatis與MybatisPlus方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
springboot操作阿里云OSS實(shí)現(xiàn)文件上傳,下載,刪除功能
這篇文章主要介紹了springboot操作阿里云OSS實(shí)現(xiàn)文件上傳,下載,刪除功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot處理跨域請(qǐng)求(CORS)的五種方式
跨域資源共享(CORS)是現(xiàn)代Web開發(fā)中常見的問題,Spring?Boot提供了多種方式來處理CORS請(qǐng)求,下面我將詳細(xì)介紹各種實(shí)現(xiàn)方式及其適用場(chǎng)景,需要的朋友可以參考下2025-04-04
SpringBoot+Dubbo+Zookeeper知識(shí)整合過程詳解
本文首先介紹了分布式系統(tǒng)的基本概念和分類,包括單一應(yīng)用架構(gòu)、垂直應(yīng)用架構(gòu)、分布式服務(wù)架構(gòu)和流動(dòng)計(jì)算架構(gòu),通過一個(gè)完整的Spring Boot + Dubbo + Zookeeper框架搭建示例,展示了如何將這些技術(shù)整合到一個(gè)實(shí)際的項(xiàng)目中,感興趣的朋友一起看看吧2025-02-02
基于Springboot+Netty實(shí)現(xiàn)rpc的方法 附demo
這篇文章主要介紹了基于Springboot+Netty實(shí)現(xiàn)rpc功能,在父項(xiàng)目中引入相關(guān)依賴結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02

