代码:
package ProcessFault.config;
import lombok.extern.slf4j.Slf4j;
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;
@Slf4j
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) {
for (int i = 0; i < 100; i++) {
Map<String, String> result = getNetworkDownUp();
System.out.println(i+"下载速度:" + result.get("rxPercent") + "kb/s 上传速度:" + result.get("txPercent") + "kb/s");
}
}
}
说明:
System.getProperties包含的方法属性
public static void main(String[] args) {
Properties props = System.getProperties();
System.out.println("Java的运行环境版本:"+props.getProperty("java.version"));
System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor"));
System.out.println("Java的运行环境供应商URL:"+props.getProperty("java.vendor.url"));
System.out.println("Java的安装路径:"+props.getProperty("java.home"));
System.out.println("Java的虚拟机规范名称:"+props.getProperty("java.vm.specification.name"));
System.out.println("Java的虚拟机实现版本:"+props.getProperty("java.vm.version"));
System.out.println("Java的虚拟机实现供应商:"+props.getProperty("java.vm.vendor"));
System.out.println("Java的虚拟机实现名称:"+props.getProperty("java.vm.name"));
System.out.println("Java的类格式版本号:"+props.getProperty("java.class.version"));
System.out.println("Java的类路径:"+props.getProperty("java.class.path"));
System.out.println("加载库时搜索的路径列表:"+props.getProperty("java.library.path"));
System.out.println("默认的临时文件路径:"+props.getProperty("java.io.tmpdir"));
System.out.println("一个或多个扩展目录的路径:"+props.getProperty("java.ext.dirs"));
System.out.println("操作系统的名称:"+props.getProperty("os.name"));
System.out.println("操作系统的构架:"+props.getProperty("os.arch"));
System.out.println("操作系统的版本:"+props.getProperty("os.version"));
System.out.println("分件分隔符:"+props.getProperty("file.separator"));//在 unix 系统中是"/"
System.out.println("路径分隔符:"+props.getProperty("path.separator"));//在 unix 系统中是":"
System.out.println("行分隔符:"+props.getProperty("line.separator"));//在 unix 系统中是"/n"
System.out.println("用户的账户名称:"+props.getProperty("user.name"));
System.out.println("用户的主目录:"+props.getProperty("user.home"));
System.out.println("用户的当前工作主目录:"+props.getProperty("user.dir"));
}
Runtime.getRuntime().exec()的使用以及简单封装
API相关
五个常用的API:
1.Runtime.getRuntime().exec(String cmd)
直接输入一行cmd命令,例如:ipconfig /all 、java -version等
2.Runtime.getRuntime().exec(String [] cmd)
将命令分开输入,例如new String[] {“java”, “-version”}
3.getInputStream();
获得输入流
4.getErrorStream();
获得错误流
5.waitFor(timeout, TimeUnit);
防止卡死