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获取上下行网速的更多相关文章

  1. Ubuntu 16.04 标题栏实时显示上下行网速、CPU及内存使用率--indicator-sysmonitor

    ---------------------------------------------------------------------------- 原文地址:http://blog.csdn.N ...

  2. Ubuntu 14.04 标题栏实时显示上下行网速、CPU及内存使用情况

    首先当然是用wget下载indicator-sysmonitor,终端执行命令:wget -c https://launchpad.net/indicator-sysmonitor/trunk/4.0 ...

  3. Ubuntu 16.04 标题栏实时显示上下行网速、CPU及内存使用率

    有时感觉网络失去响应,就通过Ubuntu 14.04自带的系统监视器程序来查看当前网速,但是这样很不方便,遂打算让网速显示在标题栏,那样就随时可直观的看到.一番搜索尝试后,成功实现!同时也实现了CPU ...

  4. Ubuntu 16.04安装indicator-sysmonitor实现导航条显示上下行网速/CPU/内存使用率

    安装: sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor sudo apt-get update sudo apt-get in ...

  5. 在mac上显示网速的软件——iStat Menus 5:

    在mac上显示网速的软件——iStat Menus 5: https://bjango.com/mac/istatmenus/ 注册码: Email: 982092332@qq.com SN: GAW ...

  6. linux上限值网速、限值带宽

    Linux操作系统中的流量控制器TC(Traffic Control)用于Linux内核的流量控制,主要是通过在输出端口处建立一个队列来实现流量控制. Linux流量控制的基本原理如下图所示. 接收包 ...

  7. java获取上个星期第一天和最后一天

    package com.goldcn.jzgmanageplat.b2b.controller; import java.text.SimpleDateFormat;import java.util. ...

  8. java获取本机外网ip

    public static String getV4IP(){ String ip = ""; String chinaz = "http://ip.chinaz.com ...

  9. 让你的Ubuntu也能像Windows那样显示网速和CPU温度

    致力于Linux桌面操作系统的平民化,一直强迫自己完全在Ubuntu系统下进行日常的电脑使用,但是用长了时间的Windows,还是有些习惯改不过来,比如只要在下载或者看在线视频的时候就会不自觉关注网速 ...

随机推荐

  1. Oracle带输入输出参数的存储过程

    (一)使用输入参数 需求:在emp_copy中添加一条记录,empno为已有empno的最大值+1,ename不能为空且长度必须大于0,deptno为60. 创建存储过程: create or rep ...

  2. OSGI 理论知识

    下面列出了主要的控制台命令: 表 1. Equinox OSGi 主要的控制台命令表 类别 命令 含义 控制框架 launch 启动框架 shutdown 停止框架 close 关闭.退出框架 exi ...

  3. canal安装与使用

    安装 alpha的版本不是稳定的版本 wget https://github.com/alibaba/canal/releases/download/canal-1.1.4/canal.deploye ...

  4. testng 执行多个suite

    我们知道testng的配置文件,一个.xml里面只能有一个suite,那么如果想要设置多个suite怎么弄呢?这个时候我们需要用到testng的标签<suite-files>. 下面说一下 ...

  5. proxy跨域

    跨域 浏览器访问非同源的网址时,会被限制访问,出现跨域问题. 解决方案: 1.response 添加 header(CORS) 2.JSONP 方式 3.全局对象+iframe (1)document ...

  6. 转:Intent 操作常用URI代码示例

    以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent 一.打开一个网页,类别是Intent.ACTION_VIEW 1 2 Uri uri = Uri.parse(&q ...

  7. [BUUCTF]REVERSE——[FlareOn4]IgniteMe

    [FlareOn4]IgniteMe 附件 步骤: 例行检查,32位程序,无壳 32位ida载入 当满足第10行的if条件时,输出G00d j0b!提示我们成功,看一下sub_401050函数 3.s ...

  8. ctfshow 红包题 武穆遗书

    偶然见看到这道题,就下载了看了看.确实是我自己的逆向能力不够,逆不动.但是我似乎找到了非预期... 下载程序,ida打开,发现不对.后来发现是加了upx壳,拿软件去一下.再次ida打开. 其中buff ...

  9. ffmpeg 系列博客

    https://www.ffmpeg.org/download.html#build-macffmpeg 系列博文https://me.csdn.net/blog/leixiaohua1020http ...

  10. Django的Form表单验证

    Form(from django import forms) 简短理解:后端提供了一个类:from django import forms,继承此类定义子类.子类中定义和form表单中提交到name名 ...