西藏巴青项目

Ip2RegionUtils.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.ruoyi.common.utils.ip;
  2. import java.io.InputStream;
  3. import org.lionsoul.ip2region.service.Config;
  4. import org.lionsoul.ip2region.service.Ip2Region;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import com.ruoyi.common.utils.StringUtils;
  8. /**
  9. * ip2region 离线 IP 归属地查询工具。
  10. */
  11. public final class Ip2RegionUtils
  12. {
  13. private static final Logger log = LoggerFactory.getLogger(Ip2RegionUtils.class);
  14. private static final String XDB_CLASSPATH = "/ip2region/ip2region_v4.xdb";
  15. private static volatile Ip2Region ip2Region;
  16. private static volatile boolean initAttempted;
  17. private Ip2RegionUtils()
  18. {
  19. }
  20. /**
  21. * 查询 IP 归属地原始字符串,格式:Country|Province|City|ISP|Code。
  22. */
  23. public static String searchRegion(String ip)
  24. {
  25. if (StringUtils.isEmpty(ip))
  26. {
  27. return null;
  28. }
  29. Ip2Region service = getIp2Region();
  30. if (service == null)
  31. {
  32. return null;
  33. }
  34. try
  35. {
  36. return service.search(ip.trim());
  37. }
  38. catch (Exception ex)
  39. {
  40. log.warn("ip2region 查询失败 ip={}: {}", ip, ex.getMessage());
  41. return null;
  42. }
  43. }
  44. /**
  45. * 从 ip2region 结果中提取可用于区县展示的名称。
  46. */
  47. public static String extractDistrictName(String region)
  48. {
  49. if (StringUtils.isEmpty(region))
  50. {
  51. return null;
  52. }
  53. String[] parts = region.split("\\|");
  54. String city = parts.length > 2 ? nullIfZero(parts[2]) : null;
  55. String province = parts.length > 1 ? nullIfZero(parts[1]) : null;
  56. String country = parts.length > 0 ? nullIfZero(parts[0]) : null;
  57. return firstNonEmpty(city, province, country);
  58. }
  59. private static Ip2Region getIp2Region()
  60. {
  61. if (!initAttempted)
  62. {
  63. synchronized (Ip2RegionUtils.class)
  64. {
  65. if (!initAttempted)
  66. {
  67. initAttempted = true;
  68. ip2Region = createIp2Region();
  69. }
  70. }
  71. }
  72. return ip2Region;
  73. }
  74. private static Ip2Region createIp2Region()
  75. {
  76. try (InputStream inputStream = Ip2RegionUtils.class.getResourceAsStream(XDB_CLASSPATH))
  77. {
  78. if (inputStream == null)
  79. {
  80. log.warn("ip2region 数据库未找到: {}", XDB_CLASSPATH);
  81. return null;
  82. }
  83. Config v4Config = Config.custom()
  84. .setCachePolicy(Config.BufferCache)
  85. .setXdbInputStream(inputStream)
  86. .asV4();
  87. return Ip2Region.create(v4Config, null);
  88. }
  89. catch (Exception ex)
  90. {
  91. log.error("ip2region 初始化失败", ex);
  92. return null;
  93. }
  94. }
  95. private static String nullIfZero(String value)
  96. {
  97. if (StringUtils.isEmpty(value) || "0".equals(value.trim()))
  98. {
  99. return null;
  100. }
  101. return value.trim();
  102. }
  103. private static String firstNonEmpty(String... values)
  104. {
  105. if (values == null)
  106. {
  107. return null;
  108. }
  109. for (String value : values)
  110. {
  111. if (StringUtils.isNotEmpty(value))
  112. {
  113. return value;
  114. }
  115. }
  116. return null;
  117. }
  118. }