AddressUtils.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package com.ruoyi.common.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.ruoyi.common.utils.http.HttpUtils;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. /**
  7. * 获取地址类
  8. *
  9. * @author ruoyi
  10. */
  11. public class AddressUtils
  12. {
  13. private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
  14. public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
  15. public static String getRealAddressByIP(String ip)
  16. {
  17. String address = "";
  18. try
  19. {
  20. address = HttpUtils.sendPost(IP_URL, "ip=" + ip);
  21. JSONObject json = JSONObject.parseObject(address);
  22. JSONObject object = json.getObject("data", JSONObject.class);
  23. String region = object.getString("region");
  24. String city = object.getString("city");
  25. address = region + " " + city;
  26. }
  27. catch (Exception e)
  28. {
  29. log.error("根据IP获取所在位置----------错误消息:" + e.getMessage());
  30. }
  31. return address;
  32. }
  33. }