取得应用服务器端的IP地址(Windows Unix Linux)

取得应用服务器端的IP地址
参考
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037
http://faq.csdn.net/read/3301.html

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

public class LinuxInetAddress {
	/**
	 * 遍历本机网卡中所有的IP地址,Linux操作系统对应
	 */
	public static String getLocalIpAddress() {
		String result = "";
		try {
			// 根据网卡取本机配置的 IP
			Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
			InetAddress ip = InetAddress.getLocalHost();
			while (netInterfaces.hasMoreElements()) {
				NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
				ip = (InetAddress) ni.getInetAddresses().nextElement();
				// Reference
				// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037
				// !ip.isSiteLocalAddress() && is deleted by zcwang
				if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
					result = ip.getHostAddress();
					break;
				} else {
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}

你可能感兴趣的:(java,应用服务器,linux,windows,unix)