AddressUtils.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.dgtly.common.utils;
  2. import com.dgtly.common.utils.http.HttpUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import com.dgtly.common.config.Global;
  6. import com.dgtly.common.json.JSON;
  7. import com.dgtly.common.json.JSONObject;
  8. /**
  9. * 获取地址类
  10. *
  11. * @author ruoyi
  12. */
  13. public class AddressUtils
  14. {
  15. private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
  16. public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
  17. public static String getRealAddressByIP(String ip)
  18. {
  19. String address = "XX XX";
  20. // 内网不查询
  21. if (IpUtils.internalIp(ip))
  22. {
  23. return "内网IP";
  24. }
  25. if (Global.isAddressEnabled())
  26. {
  27. String rspStr = HttpUtils.sendPost(IP_URL, "ip=" + ip);
  28. if (StringUtils.isEmpty(rspStr))
  29. {
  30. log.error("获取地理位置异常 {}", ip);
  31. return address;
  32. }
  33. JSONObject obj;
  34. try
  35. {
  36. obj = JSON.unmarshal(rspStr, JSONObject.class);
  37. JSONObject data = obj.getObj("data");
  38. String region = data.getStr("region");
  39. String city = data.getStr("city");
  40. address = region + " " + city;
  41. }
  42. catch (Exception e)
  43. {
  44. log.error("获取地理位置异常 {}", ip);
  45. }
  46. }
  47. return address;
  48. }
  49. }