JAVA获取上下行网速
JAVA获取上下行网速
package com.iecas.zwterminalstate.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.StringTokenizer;
public class NetWorkUtil {
private static final int SLEEP_TIME = 2 * 1000;
//获取网络上行下行速度
public static Map<String, String> getNetworkDownUp() {
Properties props = System.getProperties();
String os = props.getProperty("os.name").toLowerCase();
os = os.startsWith("win") ? "windows" : "linux";
Map<String, String> result = new HashMap<>();
Process pro = null;
Runtime r = Runtime.getRuntime();
BufferedReader input = null;
String rxPercent = "";
String txPercent = "";
try {
String command = "windows".equals(os) ? "netstat -e" : "ifconfig";
pro = r.exec(command);
input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
long result1[] = readInLine(input, os);
Thread.sleep(SLEEP_TIME);
pro.destroy();
input.close();
pro = r.exec(command);
input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
long result2[] = readInLine(input, os);
rxPercent = formatNumber((result2[0] - result1[0]) / (1024.0 * (SLEEP_TIME / 1000))); // 下行速率(kB/s)
txPercent = formatNumber((result2[1] - result1[1]) / (1024.0 * (SLEEP_TIME / 1000))); // 上行速率(kB/s)
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Optional.ofNullable(pro).ifPresent(p -> p.destroy());
}
result.put("rxPercent", rxPercent);// 下行速率
result.put("txPercent", txPercent);// 上行速率
return result;
}
private static long[] readInLine(BufferedReader input, String osType) {
long arr[] = new long[2];
StringTokenizer tokenStat = null;
try {
if (osType.equals("linux")) { // 获取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.indexOf("RX packets") >= 0) {
rx += Long.parseLong(line.substring(line.indexOf("RX packets") + 11, line.indexOf(" ", line.indexOf("RX packets") + 11)));
} else if (line.indexOf("TX packets") >= 0) {
tx += Long.parseLong(line.substring(line.indexOf("TX packets") + 11, line.indexOf(" ", line.indexOf("TX packets") + 11)));
}
}
arr[0] = rx;
arr[1] = tx;
} else { // 获取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());
}
} catch (Exception e) {
e.printStackTrace();
}
return arr;
}
private static String formatNumber(double f) {
return new Formatter().format("%.2f", f).toString();
}
public static void main(String[] args) {
Map<String, String> result = getNetworkDownUp();
System.out.println(result.get("rxPercent"));
System.out.println(result.get("txPercent"));
}
}
JAVA获取上下行网速的更多相关文章
- Ubuntu 16.04 标题栏实时显示上下行网速、CPU及内存使用率--indicator-sysmonitor
---------------------------------------------------------------------------- 原文地址:http://blog.csdn.N ...
- Ubuntu 14.04 标题栏实时显示上下行网速、CPU及内存使用情况
首先当然是用wget下载indicator-sysmonitor,终端执行命令:wget -c https://launchpad.net/indicator-sysmonitor/trunk/4.0 ...
- Ubuntu 16.04 标题栏实时显示上下行网速、CPU及内存使用率
有时感觉网络失去响应,就通过Ubuntu 14.04自带的系统监视器程序来查看当前网速,但是这样很不方便,遂打算让网速显示在标题栏,那样就随时可直观的看到.一番搜索尝试后,成功实现!同时也实现了CPU ...
- Ubuntu 16.04安装indicator-sysmonitor实现导航条显示上下行网速/CPU/内存使用率
安装: sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor sudo apt-get update sudo apt-get in ...
- 在mac上显示网速的软件——iStat Menus 5:
在mac上显示网速的软件——iStat Menus 5: https://bjango.com/mac/istatmenus/ 注册码: Email: 982092332@qq.com SN: GAW ...
- linux上限值网速、限值带宽
Linux操作系统中的流量控制器TC(Traffic Control)用于Linux内核的流量控制,主要是通过在输出端口处建立一个队列来实现流量控制. Linux流量控制的基本原理如下图所示. 接收包 ...
- java获取上个星期第一天和最后一天
package com.goldcn.jzgmanageplat.b2b.controller; import java.text.SimpleDateFormat;import java.util. ...
- java获取本机外网ip
public static String getV4IP(){ String ip = ""; String chinaz = "http://ip.chinaz.com ...
- 让你的Ubuntu也能像Windows那样显示网速和CPU温度
致力于Linux桌面操作系统的平民化,一直强迫自己完全在Ubuntu系统下进行日常的电脑使用,但是用长了时间的Windows,还是有些习惯改不过来,比如只要在下载或者看在线视频的时候就会不自觉关注网速 ...
随机推荐
- @RequestBody配合@Valid 校验入参参数
自定义一个Controller import com.example.demo.pojo.User; import org.springframework.web.bind.annotation.Po ...
- Give You My Best Wishes
亲耐滴IT童鞋们: 感谢大家一直以来的支持,因为有你们的支持,才有我这么"拼"的动力!!爱你们哟 OC的学习已经告一段落,希望大家通过阅读这几篇浅薄的随笔,能够寻找到解决问题的方法 ...
- When should we write our own copy constructor?
Reference:http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html Please write ...
- Java Log4j 配置文件
### 设置### log4j.rootLogger = debug,stdout,D,E ### 输出信息到控制抬 ### log4j.appender.stdout = org.apache.lo ...
- 【C/C++】01背包问题/动态规划
按小蓝书上写的大数据情况下没过,按解答区一个大佬的修改了过了 #include <bits/stdc++.h> using namespace std; class Solution { ...
- springboot项目启动慢,怀疑jdk有问题
项目启动慢,并且没有启动日志,开发环境和windows服务器都正常,到linux后出现问题,你觉得会是哪儿的问题? 最近收到一位同事求助,说springboot应用在客户的一台Linux机器上启动非常 ...
- 【Fastjson】Fastjson反序列化由浅入深
Fastjson真-简-介 fastjson是由alibaba开发并维护的一个json工具,以其特有的算法,号称最快的json库 fastjson的使用 首先先创一个简单的测试类User public ...
- intelliJ破解及JavaEE搭建
intellij2020.3破解 转载自https://www.exception.site/essay/how-to-free-use-intellij-idea-2019-3 第一步: 下载最新的 ...
- uWSGI和WSGI之间的关系
一.WSGI 协议 WSGI:是一种协议规范,起到规范参数的作用,就像告诉公路一样,规定超车靠右行,速度不低于90km/h,等.但这一切都是对双方进行沟通,比如,重庆到武汉这条高速路,这儿重庆和武汉就 ...
- Python列表简介和遍历
一.Python3列表简介 1.1.Python列表简介 序列是Python中最基本的数据结构 序列中的每个值都有对应的位置值,称之为索引,第一个索引是0,第二个索引是1,以此类推. Python有6 ...