`
羊小绵
  • 浏览: 50190 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
文章分类
社区版块
存档分类
最新评论

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;
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics