java 获取服务器端ip

public static String getLocalhostIP(){
        InetAddress address = null;//获取的是本地的IP地址
        try {
            address = InetAddress.getLocalHost();
            return address.getHostAddress();//192.168.0.121
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        try {
            String ip= Inet4Address.getLocalHost().getHostAddress();
            return  ip;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return "127.0.0.1";
    }

你可能感兴趣的:(java,JAVA)