获取本机IP的方法(Windows和Linux通用)

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Properties;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SystemUtil {


	private static Logger LOG = LoggerFactory.getLogger(SystemUtil.class);


	/**
	 * 返回本机IP的字符串形式
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String getLocalIp() throws Exception {
		InetAddress inet;


		inet = getSystemLocalIP();
		if (null != inet) {
			String ip = inet.getHostAddress();
			LOG.info("获取的本机IP为{}", ip);
			return ip;
		}
		throw new Exception("获取ip失败");
	}


	/**
	 * 获取本机ip的InetAddress形式
	 * 
	 * @return
	 * @throws Exception
	 */
	private static InetAddress getSystemLocalIP() throws Exception {


		InetAddress inet = null;
		String osName = getSystemOsName();
		if ("Windows".compareToIgnoreCase(osName) < 0) {
			inet = getWinLocalIp();
		} else {
			inet = getUnixLocalIp();
		}
		return inet;
	}


	/**
	 * 获取类Unix系统的IP
	 * 
	 * @return
	 * @throws Exception
	 */
	private static InetAddress getUnixLocalIp() throws Exception {
		Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
		if (null == netInterfaces) {
			throw new Exception("获取类Unix系统的IP失败");
		}
		InetAddress ip = null;
		while (netInterfaces.hasMoreElements()) {
			NetworkInterface ni = netInterfaces.nextElement();
			if (ni.isUp()) {
				Enumeration<InetAddress> addressEnumeration = ni.getInetAddresses();
				while (addressEnumeration.hasMoreElements()) {
					ip = addressEnumeration.nextElement();
					if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
						return ip;
					}
				}
			}
		}
		throw new Exception("获取类Unix系统的IP失败");
	}


	/**
	 * 获取window系统的ip,貌似不会返回null
	 * 
	 * @return
	 * @throws UnknownHostException
	 */
	private static InetAddress getWinLocalIp() throws UnknownHostException {
		InetAddress inet = InetAddress.getLocalHost();
		return inet;
	}


	/**
	 * 获取操作系统名称
	 * 
	 * @return
	 */
	private static String getSystemOsName() {
		Properties props = System.getProperties();
		String osName = props.getProperty("os.name");
		return osName;
	}


	/**
	 * 获取内存信息
	 * 
	 * @return
	 */
	public static final String getRamInfo() {
		Runtime rt = Runtime.getRuntime();
		return "RAM:" + rt.totalMemory()/1024/1024 + "MB total," + rt.freeMemory()/1024/1024 + "MB free.";
	}


	public static void main(String[] args) {
		try {
			getLocalIp();
		} catch (Exception e) {
			LOG.error("failed");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值