content: package com.ruoyi.common.utils.ip;
  
  import com.ruoyi.common.config.RuoYiConfig;
  import com.ruoyi.common.utils.RegionUtil;
  import com.ruoyi.common.utils.StringUtils;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  /**
   * 获取地址类
   *
   * @author ruoyi
   */
  public class AddressUtils {
      private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
  
      // 未知地址
      public static final String UNKNOWN = "XX XX";
  
      public static String getRealAddressByIP(String ip) {
          String address = UNKNOWN;
          // 内网不查询
          if (IpUtils.internalIp(ip)) {
              return "内网IP";
          }
          if (RuoYiConfig.isAddressEnabled()) {
              try {
                  String rspStr = RegionUtil.getRegion(ip);
                  if (StringUtils.isEmpty(rspStr)) {
                      log.error("获取地理位置异常 {}", ip);
                      return UNKNOWN;
                  }
                  String[] obj = rspStr.split("\\|");
                  String region = obj[2];
                  String city = obj[3];
  
                  return String.format("%s %s", region, city);
              } catch (Exception e) {
                  log.error("获取地理位置异常 {}", e);
              }
          }
          return address;
      }
  }
  