andyjojo 发表于 2013-2-7 03:32:56

获取本地 ip (多网卡的情况下)

public String getLocalHostName() {
      String hostName;
      try {
            InetAddress addr = InetAddress.getLocalHost();
            hostName = addr.getHostAddress();
      } 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;
                for (int i = 0; i < addrs.length; i++) {
                  ret = addrs.getHostAddress();
                }
            }
      }

      } catch (Exception ex) {
            ret = null;
      }
      return ret;
    }
页: [1]
查看完整版本: 获取本地 ip (多网卡的情况下)