获取本机IP地址代码

来源:互联网 发布:西门子s7 300编程软件 编辑:程序博客网 时间:2024/06/04 19:46
    public String getLocalIpAddress() {        try {            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();     en.hasMoreElements();) {                NetworkInterface intf = en.nextElement();                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();     enumIpAddr.hasMoreElements();) {                    InetAddress inetAddress = enumIpAddr.nextElement();                    if (!inetAddress.isLoopbackAddress()) {                        return inetAddress.getHostAddress().toString();                    }                }            }        } catch (SocketException e) {        e.printStackTrace();        }        return null;    }