/** * Project: doris.common-0.1.0-SNAPSHOT * * File Created at 2011-7-8 * $Id$ * * Copyright 1999-2100 Alibaba.com Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Alibaba Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Alibaba.com. */ package com.alibaba.doris.common.util; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; /** * TODO Comment of IPAddressUtil * * @author mian.hem */ public final class IPAddressUtil { /** * 如果没有配置外网ip,则返回本地ip */ public static String getIPAddress() { String localip = null;// 本地IP,如果没有配置外网IP则返回它 String netip = null;// 外网IP try { Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean found = false;// 是否找到外网IP while (netInterfaces.hasMoreElements() && !found) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = address.nextElement(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 外网IP netip = ip.getHostAddress(); found = true; break; } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 内网IP localip = ip.getHostAddress(); } } } } catch (SocketException e) { throw new IllegalStateException(e); } if (netip != null && !"".equals(netip)) { return netip; } else { return localip; } } }