package com.jaydenxiao.common.commonutils; import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; /** * des:ip管理 * Created by xsf * on 2016.06.16:27 */ public class IpUtils { /** * 获取ip地址方法 * @return */ public static String GetHostIp() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr .hasMoreElements();) { InetAddress inetAddress = ipAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { //if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { } catch (Exception e) { } return null; } }