```
/**
* @author 張躍帥
* @Description: 獲取網絡速度-工具
* @date 2020/08/12
*/
@Slf4j
public class NetworkUtil {
/**
* 網速測速時間2s
*/
private static final int SLEEP_SECONDS = 2;
/**
* 獲取網絡上下行速率
* 格式{"UP": "123KB/S, "DOWN": "345KB/S"}
*/
public static Dict getNetworkUpRate() {
// 創建map
Dict dictMap = Dict.create();
// 變量-過程
Process pro = null;
// 獲取運行時
Runtime r = Runtime.getRuntime();
// 變量-緩沖區讀取
BufferedReader input = null;
try {
// 獲取當前的操作系統
boolean isWindows = SystemUtil.getOsInfo().isWindows();
// windows命令
String command = isWindows ? "netstat -e" : "ifconfig";
// 執行命令
pro = r.exec(command);
// 創建緩沖讀卡器
input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
// 聯機讀取
long[] result1 = readInLine(input, isWindows);
// 等待
Thread.sleep(SLEEP_SECONDS * 1000);
// 銷毀
pro.destroy();
// 關閉
input.close();
// 執行命令
pro = r.exec(command);
// 創建緩沖讀卡器
input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
// 聯機讀取
long[] result2 = readInLine(input, isWindows);
// 可讀文件大小
String upSpeed = FileUtil.readableFileSize(Convert.toLong(NumberUtil.div(NumberUtil.sub(result2[0], result1[0]), SLEEP_SECONDS)));
String downSpeed = FileUtil.readableFileSize(Convert.toLong(NumberUtil.div(NumberUtil.sub(result2[1], result1[1]), SLEEP_SECONDS)));
// 添加
dictMap.put("up", upSpeed + (upSpeed.endsWith("B") ? "/S" : "B/S"));
dictMap.put("down", downSpeed + (downSpeed.endsWith("B") ? "/S" : "B/S"));
} catch (Exception e) {
log.info(">>> 獲取網絡測速失敗", e.getMessage());
} finally {
// 判斷
if (input != null) {
try {
// 關閉
input.close();
} catch (IOException e) {
log.info(">>> 獲取網絡測速失敗", e.getMessage());
}
}
// 使用Optional是用來設置默認值的,杜絕null的出現
Optional.ofNullable(pro).ifPresent(Process::destroy);
}
// 返回
return dictMap;
}
/**
* 格式數字
*/
@SuppressWarnings("all")
private static String formatNumber(double f) {
return new Formatter().format("%.2f", f).toString();
}
/**
* 聯機讀取
*/
private static long[] readInLine(BufferedReader input, boolean isWindows) {
// 變量數組
long[] arr = new long[2];
// 變量-字符串標記器
StringTokenizer tokenStat;
try {
// 判斷-操作系統
if (isWindows) {
// 獲取windows環境下的網口上下行速率
input.readLine();
input.readLine();
input.readLine();
input.readLine();
// 創建-字符串標記器
tokenStat = new StringTokenizer(input.readLine());
// 下一個令牌
tokenStat.nextToken();
// 參數轉換
arr[0] = Long.parseLong(tokenStat.nextToken());
arr[1] = Long.parseLong(tokenStat.nextToken());
} else {
// 獲取linux環境下的網口上下行速率
long rx = 0, tx = 0;
// 變量
String line = null;
// RX packets:4171603 errors:0 dropped:0 overruns:0 frame:0
// TX packets:4171603 errors:0 dropped:0 overruns:0 carrier:0
// 遍歷
while ((line = input.readLine()) != null) {
// 判斷
if (line.contains("RX packets")) {
// 拼接
rx += Long.parseLong(line.substring(line.indexOf("RX packets") + 11, line.indexOf(" ", line.indexOf("RX packets") + 11)));
} else if (line.contains("TX packets")) {
// 拼接
tx += Long.parseLong(line.substring(line.indexOf("TX packets") + 11, line.indexOf(" ", line.indexOf("TX packets") + 11)));
}
}
// 賦值
arr[0] = rx;
arr[1] = tx;
}
} catch (Exception e) {
log.info(">>> 獲取網絡測速失敗", e.getMessage());
}
// 返回
return arr;
}
}
- Jump簡介
- 技術架構
- 代碼規范
- 規范導讀
- JAVA規范
- 數據庫表設計規范
- 集成項目
- JDK1.8-pom.xml
- JDK21-pom.xml
- 項目結構
- 業務模塊-方法名稱規范
- 跨域配置
- License授權配置
- 公共字段自動填充
- 全局異常處理器
- PageOffice配置
- Beetl模板引擎配置
- application.properties
- application-prod.yml
- banner.txt
- logback-spring.xml
- jump-core (核心組件)
- Maven依賴
- 通用枚舉
- 公共數據狀態 - 枚舉
- 公共邏輯刪除 - 枚舉
- 公共操作編碼類型 - 枚舉
- 公共tree父節點 - 枚舉
- 公共是或否 - 枚舉
- 工具Util
- AopTargetUtil
- DownloadUtil
- GenerateNumUtil
- HttpServletUtil
- IpUtil
- JoinPointUtil
- MacUtil
- NetworkUtil
- ParamToUtil
- ResponseUtil
- TimeZoneDateUtil
- UaUtil
- 統一返回
- 結果對象
- 如何使用
- jump-cahche (緩存組件)
- Maven依賴
- Redis配置
- 緩存常量
- 工具Util
- RedisCacheUtil
- jump-idempotent (幕等組件)
- Maven依賴
- Context上下文
- 操作器
- 接口
- 如何實現
- AOP參數
- AOP使用方法
- jump-lock (分布式鎖組件)
- Maven依賴
- 枚舉
- AOP參數
- AOP使用方法
- 工具Util
- RedissonLockUtil
- Util使用方法
- jump-mybatis (mybatis組件)
- Maven依賴
- 基礎Entity
- 枚舉
- 查詢類型 - 枚舉
- 條件查詢
- search
- service
- 分頁結果集
- Mapper
- MyMapper
- 使用方法
- DDL操作
- DML操作
- 工具Util
- EntityUtil
- PageUtil
- TableUtil
- jump-dynamic-datasource (多數據源組件)
- Maven依賴
- Context上下文
- 操作器
- 接口
- 如何實現
- 工具Util
- DatasourceUtil
- 如何使用
- jump-satoken (satoken組件)
- Maven依賴
- Context上下文
- 操作器
- 接口
- 如何實現
- Satoken配置信息
- SatokenUser信息
- Redis緩存操作
- SatokenRedisCache
- SatokenUserRedisCache
- 放行白名單
- jump-oss (OSS組件)
- Maven依賴
- 工具Util
- OssFileUtil
- OssPlatformUtil
- 如何使用
- jump-xss (XSS組件)
- Maven依賴
- 白名單放行
- jump-email (郵件組件)
- Maven依賴
- Email客戶端信息
- Email發送參數
- 工具Util
- jump-websocket (WebSocket組件)
- Maven依賴
- 消息對象
- 工具Util
- 如何使用
- jump-weixin (微信組件)
- Maven依賴
- jump-system (系統管理組件)
- Maven依賴
- AOP
- 系統操作日志AOP
- 系統數據日志AOP
- 系統操作權限AOP
- 字典轉文本AOP
- Redis緩存操作
- SystemConfigRedisCache
- 工具Util
- LoginUserUtil
- SystemAreaUtil
- SystemHomeUtil
- SystemMenuUtil
- SystemOrgAdminUtil
- SystemOrgTypeUtil
- SystemRoleUtil
- SystemUserLoginAreaUtil
- SystemUserUtil
- jump-timer(定時器組件)
- Maven依賴
- 枚舉
- Api接口
- 工具Util
- ActionClassUtil
- TimerTaskUtil
- 如何使用
- jump-ueditor (富文本組件)
- Maven依賴
- 如何使用
- 配置 ueditor.config.js
- 后端 application.properties
- 前端 vue3
- vue-codemirror (代碼編譯器)
- npm依賴
- PageOffice整合
- Maven依賴
- License授權配置
- 枚舉
- 文件來源 - 枚舉
- 預覽文件類型 - 枚舉
- 文件預覽參數
- 下載文件
- 預覽文件
- 工具Util