提供些获取系统信息相关的工具方法

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);
}

}

转载于:https://www.cnblogs.com/chinaifae/p/10254896.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值