获取本机的IP //1.windows系统String ip = InetAddress.getLocalHost().getHostAddress();/*** 获取本机IP linux系统* @return 本机IP*/public static String getInternetIp() { String ip = null; try { Enumeration netWorks = NetworkInter
//1.windows系统
String ip = InetAddress.getLocalHost().getHostAddress();
/**
* 获取本机IP linux系统
* @return 本机IP
*/
public static String getInternetIp() {
String ip = null;
try {
Enumeration
netWorks = NetworkInterface.getNetworkInterfaces();
InetAddress inetAddress = null;
Enumeration
addresses; ip = InetAddress.getLocalHost().getHostAddress(); while (netWorks.hasMoreElements()) { addresses = netWorks.nextElement().getInetAddresses(); while (addresses.hasMoreElements()) { inetAddress = addresses.nextElement(); if (inetAddress != null && inetAddress instanceof Inet4Address && !inetAddress.getHostAddress().equals(ip)) { logger.info("当前主机的IP为: " + inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } catch (SocketException e) { logger.error("获取主机IP失败",e); } catch (UnknownHostException e) { logger.error("未知主机IP",e); } logger.info("当前主机的IP为: " + ip); return ip; }
