首頁>Club>
7
回覆列表
  • 1 # 使用者834195712159

    程式碼示例:

    /**

    * 獲取Linux下的IP地址

    *

    * @return IP地址

    * @throws SocketException

    */

    public static String getLinuxLocalIp() throws SocketException {

    String ip = "";

    try {

    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();

    en.hasMoreElements();) {

    NetworkInterface intf = en.nextElement();

    String name = intf.getName();

    if (!name.contains("docker") && !name.contains("lo")) {

    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();

    enumIpAddr.hasMoreElements();) {

    InetAddress inetAddress = enumIpAddr.nextElement();

    if (!inetAddress.isLoopbackAddress()) {

    String ipaddress = inetAddress.getHostAddress().toString();

    if (!ipaddress.contains("::") && !ipaddress.contains("0:0:")

    && !ipaddress.contains("fe80")) {

    ip = ipaddress;

    }

    }

    }

    }

    }

    } catch (SocketException ex) {

    System.out.println("獲取ip地址異常");

    ex.printStackTrace();

    }

    System.out.println("IP:" + ip);

    return ip;

    }

  • 2 # 呂梁668

    public String getUnixLocalIp() {

    String ip = "";

    try {

    Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();

    while (e1.hasMoreElements()) {

    NetworkInterface ni = (NetworkInterface) e1.nextElement();

    if (!ni.getName().equals("eth0")) {

    continue;

    } else {

    Enumeration<?> e2 = ni.getInetAddresses();

    while (e2.hasMoreElements()) {

    InetAddress ia = (InetAddress) e2.nextElement();

    if (ia instanceof Inet6Address)

    continue;

    ip = ia.getHostAddress();

    }

    break;

    }

    }

    } catch (SocketException e) {

    e.printStackTrace();

    System.exit(-1);

    }

    return ip;

    }

  • 中秋節和大豐收的關聯?
  • 同屬鬼谷子門下,為何秦王用張儀而不用蘇秦?