AddressUtils.java 1.3 KB

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