PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/common/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java

https://gitlab.com/mrsunchangemyselfsun/ruoyi-vue
Java | 55 lines | 42 code | 5 blank | 8 comment | 3 complexity | 72b11a4219336cee44fea5e39138fefc MD5 | raw file
  1. package com.ruoyi.common.utils.ip;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.ruoyi.common.config.RuoYiConfig;
  6. import com.ruoyi.common.constant.Constants;
  7. import com.ruoyi.common.utils.StringUtils;
  8. import com.ruoyi.common.utils.http.HttpUtils;
  9. /**
  10. * 获取地址类
  11. *
  12. * @author ruoyi
  13. */
  14. public class AddressUtils
  15. {
  16. private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
  17. // IP地址查询
  18. public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
  19. // 未知地址
  20. public static final String UNKNOWN = "XX XX";
  21. public static String getRealAddressByIP(String ip)
  22. {
  23. // 内网不查询
  24. if (IpUtils.internalIp(ip))
  25. {
  26. return "内网IP";
  27. }
  28. if (RuoYiConfig.isAddressEnabled())
  29. {
  30. try
  31. {
  32. String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
  33. if (StringUtils.isEmpty(rspStr))
  34. {
  35. log.error("获取地理位置异常 {}", ip);
  36. return UNKNOWN;
  37. }
  38. JSONObject obj = JSONObject.parseObject(rspStr);
  39. String region = obj.getString("pro");
  40. String city = obj.getString("city");
  41. return String.format("%s %s", region, city);
  42. }
  43. catch (Exception e)
  44. {
  45. log.error("获取地理位置异常 {}", ip);
  46. }
  47. }
  48. return UNKNOWN;
  49. }
  50. }