All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.zhangxd1989.basetool.system.HostInfo Maven / Gradle / Ivy

package com.github.zhangxd1989.basetool.system;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 代表当前主机的信息
 *
 * @author sheldon
 */
public class HostInfo {

    private final String HOST_NAME;
    private final String HOST_ADDRESS;

    public HostInfo() {
        String hostName;
        String hostAddress;

        try {
            InetAddress localhost = InetAddress.getLocalHost();

            hostName = localhost.getHostName();
            hostAddress = localhost.getHostAddress();
        } catch (UnknownHostException e) {
            hostName = "localhost";
            hostAddress = "127.0.0.1";
        }

        HOST_NAME = hostName;
        HOST_ADDRESS = hostAddress;
    }

    /**
     * 取得当前主机的名称。
     * 

* 例如:"webserver1" *

* * @return 主机名 */ public final String getName() { return HOST_NAME; } /** * 取得当前主机的地址。 *

* 例如:"192.168.0.1" *

* * @return 主机地址 */ public final String getAddress() { return HOST_ADDRESS; } /** * 将当前主机的信息转换成字符串。 * * @return 主机信息的字符串表示 */ @Override public final String toString() { StringBuilder builder = new StringBuilder(); SystemUtil.append(builder, "Host Name: ", getName()); SystemUtil.append(builder, "Host Address: ", getAddress()); return builder.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy