zoukankan      html  css  js  c++  java
  • 提供些获取系统信息相关的工具方法

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

    }

  • 相关阅读:
    Delphi实战中讲解FormCreate,FormShow,FormActivate
    delphi Try except on e:Exception do
    Delphi处理数据网格DBGrid的编辑框 获取还没有提交到数据集的字段文本
    delphi dbgrid中如何自动生成序号
    DBDateTimePicker;
    Delphi控件开发浅入深出(八)
    delphi中日期类型TDateTime使用总结
    在DBGrid录数据时,如何判断光标位置是在数据的最左或最右,如果是最左或最右则在按左右光标键时光标跳到上一格或下一格,如果是在数据中
    请问如何按Enter键让DBGrid的光标向右移以及换行?(0分)
    tdbgrid中用enter仿真tab键盘_delphi教程
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10254896.html
Copyright © 2011-2022 走看看