java 如何获取多网卡时的本机IP

public String getLocalHostName() {
        String hostName;
        try {
            InetAddress addr = InetAddress.getLocalHost();
            hostName = addr.getHostName();
        } catch (Exception ex) {
            hostName = "";
        }
        return hostName;
    }

public String[] getAllLocalHostIP() {
    String[] ret = null;
    try {
        String hostName = getLocalHostName();
        if (hostName.length() > 0) {
            InetAddress[] addrs = InetAddress.getAllByName(hostName);
            if (addrs.length > 0) {
                ret = new String[addrs.length];
                for (int i = 0; i < addrs.length; i++) {
                    ret[i] = addrs[i].getHostAddress();
                }
            }
        }

        } catch (Exception ex) {
            ret = null;
        }
        return ret;
    }


以上代码在WIN XP上测试通过,有机会在linux上测试一下

你可能感兴趣的:(java,exception,linux,String,测试,null)