Ver Fonte

fix: ip2region1

shc há 1 ano atrás
pai
commit
a7f8feb4d6

+ 41 - 45
hnqz-common/hnqz-common-core/src/main/java/com/qunzhixinxi/hnqz/common/core/util/Ip2Region.java

@@ -19,58 +19,54 @@ import java.util.concurrent.TimeUnit;
  */
 public final class Ip2Region {
 
-	private static final Logger log = LoggerFactory.getLogger(Ip2Region.class);
+    private static final Logger log = LoggerFactory.getLogger(Ip2Region.class);
 
-	/**
-	 * 搜索器
-	 */
-	private Searcher searcher;
+    /**
+     * 搜索器
+     */
+    private Searcher searcher;
 
-	/**
-	 * 从 dbPath 加载整个 xdb 到内存。
-	 */
-	public Ip2Region() {
+    /**
+     * 从 dbPath 加载整个 xdb 到内存。
+     */
+    public Ip2Region() {
 
-		String XDB_PATH = "lib/ip2region.xdb";
-		String TARGET = "temp/ip2region.xdb";
-		try (InputStream is = Ip2Region.class.getClassLoader().getResourceAsStream(XDB_PATH)) {
-			File target = new File(TARGET);
-			FileUtils.copyInputStreamToFile(is, target);
-			byte[] cBuff = Searcher.loadContentFromFile(target.getAbsolutePath());
-			searcher = Searcher.newWithBuffer(cBuff);
-		} catch (IOException e) {
-			log.warn("从dbPath加载整个xdb到内存失败", e);
-		}
-	}
+        try {
+            byte[] cBuff = Searcher.loadContentFromFile("/ip2region.xdb");
+            searcher = Searcher.newWithBuffer(cBuff);
+        } catch (IOException e) {
+            log.warn("从dbPath加载整个xdb到内存失败", e);
+        }
+    }
 
-	/**
-	 * 获取IP属地
-	 * <p>
-	 * 格式为:[province]·[city]
-	 * </p>
-	 *
-	 * @param remoteIp 远程IP
-	 * @return IP属地
-	 */
-	public String toRegion(final String remoteIp) throws RuntimeException {
+    /**
+     * 获取IP属地
+     * <p>
+     * 格式为:[province]·[city]
+     * </p>
+     *
+     * @param remoteIp 远程IP
+     * @return IP属地
+     */
+    public String toRegion(final String remoteIp) throws RuntimeException {
 
-		try {
+        try {
 
-			// 查询IP属地
-			long sTime = System.nanoTime();
-			String search = searcher.search(remoteIp);
-			long cost = TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - sTime);
-			log.info("{region: {}, ioCount: {}, took: {} μs}", search, searcher.getIOCount(), cost);
+            // 查询IP属地
+            long sTime = System.nanoTime();
+            String search = searcher.search(remoteIp);
+            long cost = TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - sTime);
+            log.info("{region: {}, ioCount: {}, took: {} μs}", search, searcher.getIOCount(), cost);
 
-			// 封装为固定格式[province]·[city]
-			search = search.replace("|0", "").replace("0|", "");
-			String[] split = search.split("\\|");
-			return (split.length == 2) ? split[0] + "·" + split[1] : split[1] + "·" + split[2];
+            // 封装为固定格式[province]·[city]
+            search = search.replace("|0", "").replace("0|", "");
+            String[] split = search.split("\\|");
+            return (split.length == 2) ? split[0] + "·" + split[1] : split[1] + "·" + split[2];
 
-		} catch (Exception e) {
-			log.error("获取IP属地失败", e);
-			throw new RuntimeException(e.getMessage());
-		}
-	}
+        } catch (Exception e) {
+            log.error("获取IP属地失败", e);
+            throw new RuntimeException(e.getMessage());
+        }
+    }
 
 }

+ 2 - 1
hnqz-upms/hnqz-upms-biz/src/main/java/com/qunzhixinxi/hnqz/admin/controller/ApiController.java

@@ -1274,8 +1274,9 @@ public class ApiController {
 	public R<?> saveTaskContent(@RequestBody WmTaskContent wmTaskContent, HttpServletRequest request) {
 
 		String ipAddr = HttpUtils.getIpAddr(request);
+		log.info("提交任务远端IP: [{}]", ipAddr);
 		String region = ip2Region.toRegion(ipAddr);
-		log.info("提交任务远端IP和地址: [{}]:[{}]", ipAddr, region);
+		log.info("提交任务远端地址: [{}]", region);
 		log.info("saveTaskContent入参:{}", wmTaskContent);
 		Set<String> errorHash = new HashSet<>();
 		List<String> ducImgUrl = new ArrayList<>();