/**
* 设置本地日期
* @param date yyyy-MM-dd格式
*/
private static void setSystemDate(String date){
Process process = null;
String command1 = "cmd /c date "+date;
System.out.println(command1);
try {
process = Runtime.getRuntime().exec(command1);
//必须等待该进程结束,否则时间设置就无法生效
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
}
/**
* @Purpose:采集网络带宽使用量
* @param args
* @return float,网络带宽已使用量
*/
public static Double getNetworkThoughput() {
Double curRate = 0.0;
Runtime r = Runtime.getRuntime();
// 第一次采集流量数据
long startTime = System.currentTimeMillis();
long total1 = calculateThoughout(r);
// 休眠1秒后,再次收集
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 第二次采集流量数据
long endTime = System.currentTimeMillis();
long total2 = calculateThoughout(r);
// 计算该段时间内的吞吐量:单位为Mbps(million bit per second)
double interval = (endTime-startTime)/1000;
curRate = (total2-total1)*8/1000000*interval;
System.out.println("收集网络带宽使用率结束,当前设备的网卡吞吐量为:"+(curRate)+"Mbps.");
return curRate;
}
/**
* 计算某个时刻网卡的收发数据总量
* @param runtime
* @return
*/
private static long calculateThoughout(Runtime runtime){
Process process = null;
String command = "cat /proc/net/dev";
BufferedReader reader = null;
String line = null;
long total = 0;
try {
process = runtime.exec(command);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = reader.readLine()) != null) {
line = line.trim();
// 考虑多网卡的情况
if (line.startsWith("eth")) {
log.debug(line);
line = line.substring(5).trim();
String[] temp = line.split("\\s+");
total+=(Long.parseLong(temp[0].trim()));// Receive
total+=(Long.parseLong(temp[8].trim()));// Transmit
}
}
} catch (NumberFormatException | IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (process != null) {
process.destroy();
}
}
return total;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有