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

cn.hutool.system.HostInfo Maven / Gradle / Ivy

Go to download

Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

There is a newer version: 5.8.34
Show newest version
package cn.hutool.system;

import cn.hutool.core.net.NetUtil;

import java.io.Serializable;
import java.net.InetAddress;

/**
 * 代表当前主机的信息。
 */
public class HostInfo implements Serializable {
	private static final long serialVersionUID = 1L;

	private final String HOST_NAME;
	private final String HOST_ADDRESS;

	public HostInfo() {
		final InetAddress localhost = NetUtil.getLocalhost();
		if(null != localhost){
			HOST_NAME = localhost.getHostName();
			HOST_ADDRESS = localhost.getHostAddress();
		} else{
			HOST_NAME = null;
			HOST_ADDRESS = null;
		}
	}

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

* 例如:"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