CommonNetWorkInfoUtil.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright [2022] [https://www.xiaonuo.vip]
  3. *
  4. * Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
  5. *
  6. * 1.请不要删除和修改根目录下的LICENSE文件。
  7. * 2.请不要删除和修改Snowy源码头部的版权声明。
  8. * 3.本项目代码可免费商业使用,商业使用请保留源码和相关描述文件的项目出处,作者声明等。
  9. * 4.分发源码时候,请注明软件出处 https://www.xiaonuo.vip
  10. * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
  11. * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
  12. */
  13. package vip.xiaonuo.common.util;
  14. import cn.hutool.core.convert.Convert;
  15. import cn.hutool.core.io.FileUtil;
  16. import cn.hutool.core.util.NumberUtil;
  17. import cn.hutool.system.SystemUtil;
  18. import lombok.extern.slf4j.Slf4j;
  19. import java.io.BufferedReader;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.util.*;
  23. /**
  24. * 通用获取当前网速工具类
  25. *
  26. * @author xuyuxiang
  27. * @date 2022/9/1 23:45
  28. */
  29. @Slf4j
  30. public class CommonNetWorkInfoUtil {
  31. /**
  32. * 网速测速时间2s
  33. */
  34. private static final int SLEEP_SECONDS = 2;
  35. /**
  36. * 获取网络上下行速率,格式{"UP": "123KB/S, "DOWN": "345KB/S"}
  37. *
  38. * @author xuyuxiang
  39. * @date 2022/9/1 23:51
  40. */
  41. public static Map<String, String> getNetworkUpRate() {
  42. Map<String, String> result = new HashMap<>();
  43. Process pro = null;
  44. Runtime r = Runtime.getRuntime();
  45. BufferedReader input = null;
  46. try {
  47. boolean isWindows = SystemUtil.getOsInfo().isWindows();
  48. String command = isWindows ? "netstat -e" : "ifconfig";
  49. pro = r.exec(command);
  50. input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
  51. long[] result1 = readInLine(input, isWindows);
  52. Thread.sleep(SLEEP_SECONDS * 1000);
  53. pro.destroy();
  54. input.close();
  55. pro = r.exec(command);
  56. input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
  57. long[] result2 = readInLine(input, isWindows);
  58. String upSpeed = FileUtil.readableFileSize(Convert.toLong(NumberUtil
  59. .div(NumberUtil.sub(result2[0], result1[0]), SLEEP_SECONDS)));
  60. String downSpeed = FileUtil.readableFileSize(Convert.toLong(NumberUtil
  61. .div(NumberUtil.sub(result2[1], result1[1]), SLEEP_SECONDS)));
  62. result.put("UP", upSpeed + (upSpeed.endsWith("B")?"/S":"B/S"));
  63. result.put("DOWN", downSpeed + (downSpeed.endsWith("B")?"/S":"B/S"));
  64. } catch (Exception e) {
  65. log.info(">>> 网络测速失败:", e);
  66. } finally {
  67. if (input != null) {
  68. try {
  69. input.close();
  70. } catch (IOException e) {
  71. log.info(">>> 网络测速失败:", e);
  72. }
  73. }
  74. Optional.ofNullable(pro).ifPresent(Process::destroy);
  75. }
  76. return result;
  77. }
  78. private static String formatNumber(double f) {
  79. return new Formatter().format("%.2f", f).toString();
  80. }
  81. private static long[] readInLine(BufferedReader input, boolean isWindows) {
  82. long[] arr = new long[2];
  83. StringTokenizer tokenStat;
  84. try {
  85. if (isWindows) {
  86. // 获取windows环境下的网口上下行速率
  87. input.readLine();
  88. input.readLine();
  89. input.readLine();
  90. input.readLine();
  91. tokenStat = new StringTokenizer(input.readLine());
  92. tokenStat.nextToken();
  93. arr[0] = Long.parseLong(tokenStat.nextToken());
  94. arr[1] = Long.parseLong(tokenStat.nextToken());
  95. } else {
  96. // 获取linux环境下的网口上下行速率
  97. long rx = 0, tx = 0;
  98. String line = null;
  99. //RX packets:4171603 errors:0 dropped:0 overruns:0 frame:0
  100. //TX packets:4171603 errors:0 dropped:0 overruns:0 carrier:0
  101. while ((line = input.readLine()) != null) {
  102. if (line.contains("RX packets")) {
  103. rx += Long.parseLong(line.substring(line.indexOf("RX packets") + 11, line.indexOf(" ",
  104. line.indexOf("RX packets") + 11)));
  105. } else if (line.contains("TX packets")) {
  106. tx += Long.parseLong(line.substring(line.indexOf("TX packets") + 11, line.indexOf(" ",
  107. line.indexOf("TX packets") + 11)));
  108. }
  109. }
  110. arr[0] = rx;
  111. arr[1] = tx;
  112. }
  113. } catch (Exception e) {
  114. log.error(">>> 网络测速异常:", e);
  115. }
  116. return arr;
  117. }
  118. }