获取本地IP地址

    public String getLocalIpAddress() {
        String ip = "";
        try {
            final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                final NetworkInterface intf = en.nextElement();
                final Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    final InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        ip = inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (final SocketException ex) {
            ex.printStackTrace();
        }
        return ip;
    }

你可能感兴趣的:(String)