欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java获取本地多网卡ip java多网卡ip 

程序员文章站 2022-06-23 20:50:56
...
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;
    }
相关标签: java 多网卡 ip