获取本地主机名和Ip地址 与 根据域名获取ip地址和主机名

工具类:

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.ArrayUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * @PRODUCT_NAME: IntelliJ IDEA
 * @PROJECT_NAME: ztracecore
 * @PACKAGE_NAME: com.zto.ztrace.util
 * @AUTHOR: Choj
 * @DATE: 2019-04-03 13:33
 * @NAME: AddressUtil
 * @DESRIPTION: 地址工具
 **/
public class AddressUtil {

	/**
	 * 获取 本地的IP地址
	 *
	 * @return 返回 本地的IP地址
	 */
	public static String getLocalIpAddress() {
		final InetAddress inetAddress = AddressUtil.getLocalHost();
		if (inetAddress == null) {
			return null;
		}
		return inetAddress.getHostAddress();
	}

	/**
	 * 获取 本地的主机名
	 *
	 * @return 返回 本地的主机名
	 */
	public static String getLocalHostName() {
		final InetAddress inetAddress = AddressUtil.getLocalHost();
		if (inetAddress == null) {
			return null;
		}
		return inetAddress.getHostName();
	}

	/**
	 * 获取的是该网站的地址信息
	 *
	 * @param url 请求的地址 比如 www.db.com
	 * @return 返回 获取的是该网站地址信息
	 */
	public static InetAddress getRemoteHost(final String url) {
		try {
			return InetAddress.getByName(url);
		} catch (final UnknownHostException e) {
			return null;
		}
	}

	/**
	 * 获取 远程的IP地址
	 *
	 * @return 返回 远程的IP地址
	 */
	public static String getRemoteIpAddress(final String url) {
		final InetAddress inetAddress = AddressUtil.getRemoteHost(url);
		if (inetAddress == null) {
			return null;
		}
		return inetAddress.getHostAddress();
	}

	/**
	 * 获取 远程的主机名
	 *
	 * @return 返回 远程的主机名
	 */
	public static String getRemoteHostName(final String url) {
		final InetAddress inetAddress = AddressUtil.getRemoteHost(url);
		if (inetAddress == null) {
			return null;
		}
		return inetAddress.getHostName();
	}

	/**
	 * 获取的是该网站的地址信息
	 *
	 * @param url 请求的地址 比如 www.baidu.com
	 * @return 返回 获取的是该网站地址信息
	 */
	public static InetAddress[] getRemoteHostList(final String url) {
		try {
			return InetAddress.getAllByName(url);
		} catch (final UnknownHostException e) {
			return new InetAddress[0];
		}
	}

	/**
	 * 获取 远程的IP地址
	 *
	 * @return 返回 远程的IP地址
	 */
	public static List<String> getRemoteIpAddressList(final String url) {
		final InetAddress[] addresses = AddressUtil.getRemoteHostList(url);
		if (ArrayUtils.isEmpty(addresses)) {
			return new ArrayList<>();
		}
		final List<String> ips = new ArrayList<>();
		for (final InetAddress address : addresses) {
			ips.add(address.getHostAddress());
		}
		return ips;
	}

	/**
	 * 获取 远程的主机名
	 *
	 * @return 返回 远程的主机名
	 */
	public static List<String> getRemoteHostNameList(final String url) {
		final InetAddress[] addresses = AddressUtil.getRemoteHostList(url);
		if (ArrayUtils.isEmpty(addresses)) {
			return new ArrayList<>();
		}
		final List<String> ips = new ArrayList<>();
		for (final InetAddress address : addresses) {
			ips.add(address.getHostName());
		}
		return ips;
	}

	// ---------- 私有方法 -----------

	/**
	 * 私有构造函数
	 */
	private AddressUtil() {
	}

	/**
	 * 获取本地的地址
	 *
	 * @return 返回地址 hostName/ipAddress
	 */
	private static InetAddress getLocalHost() {
		try {
			return InetAddress.getLocalHost();
		} catch (final UnknownHostException e) {
			return null;
		}
	}

	// --------- 测试 ---------

	/**
	 * 入口(测试)
	 *
	 * @param args 参数
	 */
	public static void main(final String[] args) {
		final String localHostName = AddressUtil.getLocalHostName();
		System.out.println(localHostName);

		final InetAddress localHost = AddressUtil.getLocalHost();
		System.out.println(localHost.getHostAddress());

		final String url = "www.baidu.com";
		final String remoteHostName = AddressUtil.getRemoteHostName(url);
		System.out.println(remoteHostName);

		final List<String> remoteHostNameList = AddressUtil.getRemoteHostNameList(url);
		System.out.println(JSON.toJSONString(remoteHostNameList));

		final String remoteIpAddress = AddressUtil.getRemoteIpAddress(url);
		System.out.println(remoteIpAddress);

		final List<String> remoteIpAddressList = AddressUtil.getRemoteIpAddressList(url);
		System.out.println(JSON.toJSONString(remoteIpAddressList));
	}
}

 结果:

DESKTOP-O4N5NSMO
169.214.1.163
www.baidu.com
["www.baidu.com","www.baidu.com"]
115.239.210.27
["115.239.210.27","115.239.211.112"]

 

 

相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页