서버의 IP 주소 가져오기

2050 단어 도구류
/**
 * @ClassName IpUtil
 * @Description TODO
 * @Author wushaopei
 * @Date 2019/7/22 10:15
 * @Version 1.0
 */
public class IpUtil {
    private static final Logger logger = LoggerFactory.getLogger( IpUtil.class );

    // 
    public static String getLocalIP() {
        String sIP = "";
        InetAddress ip = null;
        try {
            boolean bFindIP = false;
            Enumeration netInterfaces = (Enumeration) NetworkInterface
                    .getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                if (bFindIP) {
                    break;
                }
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                Enumeration ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = (InetAddress) ips.nextElement();
                    if (!ip.isLoopbackAddress() && ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
                        bFindIP = true;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        if (null != ip) {
            sIP = ip.getHostAddress();
        }
        return sIP;
    }
    
    // , ip 
    public static int compareIp(){
        // ip 
        String serverIp = IpUtil.getLocalIP();
        logger.info(" ip serverIp:{}",serverIp);
        // appolo ip 
        Config config = ConfigService.getConfig("YFB.alpha-game.basic");
        String appoloIp = config.getProperty("compareUrl", null);
        logger.info(" appolo ip appoloIp:{}",appoloIp);

        if(!serverIp.equals(appoloIp)){
            return 0;
        }
        return 1;
    }
}

테스트:
String s = IpUtil.getLocalIP();
 int i = IpUtil.compareIp();

좋은 웹페이지 즐겨찾기