561c15475293317645497803998ab5c85ec534c5.svn-base 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package cn.com.goldenwater.dcproj.utils.ip;
  2. import cn.com.goldenwater.dcproj.constValue.Constants;
  3. import cn.com.goldenwater.dcproj.utils.StringUtils;
  4. import cn.com.goldenwater.dcproj.utils.http.HttpUtils;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONObject;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. /**
  10. * 获取地址类
  11. *
  12. * @author ruoyi
  13. */
  14. public class AddressUtils {
  15. private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
  16. // IP地址查询
  17. public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
  18. // 未知地址
  19. public static final String UNKNOWN = "XX XX";
  20. public static String getRealAddressByIP(String ip) {
  21. // 内网不查询
  22. if (IpUtils.internalIp(ip)) {
  23. return "内网IP";
  24. }
  25. try {
  26. String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
  27. if (StringUtils.isEmpty(rspStr)) {
  28. log.error("获取地理位置异常 {}", ip);
  29. return UNKNOWN;
  30. }
  31. JSONObject obj = JSON.parseObject(rspStr);
  32. String region = obj.getString("pro");
  33. String city = obj.getString("city");
  34. return String.format("%s %s", region, city);
  35. } catch (Exception e) {
  36. log.error("获取地理位置异常 {}", ip);
  37. }
  38. return UNKNOWN;
  39. }
  40. }