提供些获取系统信息相关的工具方法
package com.opslab.util;
import com.sun.management.OperatingSystemMXBean;
import java.lang.management.ManagementFactory;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
/**
* 提供些获取系统信息相关的工具方法
*/
public class SysUtil {
/**
* JVM的版本
*/
public static final String JVM_VERSION = PropertiesUtil.key("java.version");
/**
* JVM的编码
*/
public static final String JVM_ENCODING = PropertiesUtil.key("file.encoding");
/**
* JVM默认的临时目录
*/
public static final String JVM_TEMPDIR = PropertiesUtil.key("java.io.tmpdir");
public static final String HTTP_PROXY_HOST = "http.proxyHost";
public static final String HTTP_PROXY_PORT = "http.proxyPort";
;
public static final String HTTP_PROXY_USER = "http.proxyUser";
;
public static final String HTTP_PROXY_PASSWORD = "http.proxyPassword";
/**
* 主机IP
*/
public static String HOST_IP;
/**
* 主机名
*/
public static String HOST_NAME;
/**
* 主机架构
*/
public static String OS_ARCH = PropertiesUtil.key("os.arch");
/**
* 主机类型
*/
public static String OS_NAME = PropertiesUtil.key("os.name");
/**
* 主机类型版本
*/
public static String OS_VERSION = PropertiesUtil.key("os.version");
/**
* 操作系统类型
*/
public static String SUN_DESKTOP = PropertiesUtil.key("sun.desktop");
/**
* 当前用户
*/
public static String CURRENT_USER = PropertiesUtil.key("user.name");
/**
* 当前用户的家目录
*/
public static String CURRENT_USER_HOME = PropertiesUtil.key("user.home");
/**
* 当用用户的工作目录
*/
public static String CURRENT_USER_DIR = PropertiesUtil.key("user.dir");
public static String FILE_SEPARATOR = PropertiesUtil.key("file.separator");
public static String PATH_SEPARATOR = PropertiesUtil.key("path.separator");
public static String LINE_SEPARATOR = PropertiesUtil.key("line.separator");
/**
* 总的物理内存
*/
public static long TotalMemorySize;
private static OperatingSystemMXBean osmxb;
private static int kb = 1024;
static {
try {
InetAddress addr = InetAddress.getLocalHost();
HOST_NAME = addr.getHostName();
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
if (null != netint.getHardwareAddress()) {
List<InterfaceAddress> list = netint.getInterfaceAddresses();
for (InterfaceAddress interfaceAddress : list) {
InetAddress ip = interfaceAddress.getAddress();
if (ip instanceof Inet4Address) {
HOST_IP += interfaceAddress.getAddress().toString();
}
}
}
}
HOST_IP = HOST_IP.replaceAll("null", "");
} catch (Exception e) {
System.out.println("获取服务器IP出错");
}
try {
osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
TotalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
} catch (Exception e) {
System.out.println("获取系统信息失败");
e.printStackTrace();
}
}
/**
* 已使用的物理内存
*/
public final static long usedMemory() {
if (CheckUtil.valid(osmxb)) {
return (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb;
}
return 0;
}
/**
* 获取JVM内存总量
*
*/
public final static long JVMtotalMem() {
return Runtime.getRuntime().totalMemory() / kb;
}
/**
* 虚拟机空闲内存量
*
*/
public final static long JVMfreeMem() {
return Runtime.getRuntime().freeMemory() / kb;
}
/**
* 虚拟机使用最大内存量
*
*/
public final static long JVMmaxMem() {
return Runtime.getRuntime().maxMemory() / kb;
}
/**
* Sets HTTP proxy settings.
*/
public final static void setHttpProxy(String host, String port, String username, String password) {
System.getProperties().put(HTTP_PROXY_HOST, host);
System.getProperties().put(HTTP_PROXY_PORT, port);
System.getProperties().put(HTTP_PROXY_USER, username);
System.getProperties().put(HTTP_PROXY_PASSWORD, password);
}
/**
* Sets HTTP proxy settings.
*/
public final static void setHttpProxy(String host, String port) {
System.getProperties().put(HTTP_PROXY_HOST, host);
System.getProperties().put(HTTP_PROXY_PORT, port);
}
}
相关文章
- 机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
- Java从入门到放弃18---Map集合/HashMap/LinkedHashMap/TreeMap/集合嵌套/Collections工具类常用方法
- 当前日期+随机数+文件名(采用两种方法实现的工具类)
- Java 8 默认方法和多继承深入解析
- 在js中 把 json对象转化为String对象的方法
- 基于StringUtils工具类的常用方法介绍(必看篇)
- 重写基类方法,快速实现基类纯虚方法
- Java 蒙特卡洛方法圆周率 π 值求解
- App启动速度监控-方法级别启动耗时检查工具
- JavaScript 必须学会的11 个工具方法(避免重复造轮子)
- 使用JavaScript修改伪类样式的方法
- 使用工厂方法模式设计能够实现包含加法(+)、减法(-)、乘法(*)、除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果。要求使用相关的工具绘制UML类图并严格按照类图的设计编写程序实
- ios:设置视图背景图片的方法
- 随想录(矩阵计算的几种方法)
- 基因组组装工具之 SOAPdenovo 使用方法
- 可视化工具之 IGV 使用方法
- ASP.NET Core(C#) 字符串常用处理转换的扩展方法工具类
- Python pandas.DataFrame.pipe函数方法的使用
- Java集合List按日期升序或降序四种方法
- 表迁移工具的选型-复制ibd的方法
- 有关WINDOWS XP登录密码的几种方法
- dotnet 调试应用启动闪退的方法
- Mysql SSH隧道连接使用方法
- 深入了解 Hugging Face 中的生成工具:Generate方法
- nmap端口扫描工具安装和使用方法