获取本机外网IP

版权声明:巴啦啦小魔仙乌卡拉卡,变身---------------------------------------xiner https://blog.csdn.net/zhengsaisai/article/details/88944821
package com.framework.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import cn.hutool.http.HttpUtil;

public class GetIp {

	public static String getIp() {
		String result = "127.0.0.1";
		try {
			String json = HttpUtil.get("http://pv.sohu.com/cityjson");
			String regEx = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
			Pattern p = Pattern.compile(regEx);
			Matcher m = p.matcher(json);
			while (m.find()) {
				result = m.group();
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "127.0.0.1";
		}
		return result;

	}

}

猜你喜欢

转载自blog.csdn.net/zhengsaisai/article/details/88944821