1.enable IP util

main
Gary 10 months ago
parent abffc680cc
commit 2f40ec8c2c

@ -1,4 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.luoo</groupId>
@ -98,7 +100,11 @@
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
<build>
<finalName>app</finalName>
@ -107,15 +113,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- docker的maven插件官网 https://github.com/spotify/docker-maven-plugin -->
<!-- docker的maven插件官网
https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>116.62.145.60:5000/${project.artifactId}:${project.version}</imageName>
<imageName>
116.62.145.60:5000/${project.artifactId}:${project.version}</imageName>
<baseImage>jdk1.8</baseImage>
<entryPoint>["java", "-jar","/${project.build.finalName}.jar"]</entryPoint>
<entryPoint>["java",
"-jar","/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>

@ -1,7 +1,30 @@
package com.luoo.user.util;
import java.io.InputStream;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
public class IpUtil {
private static final String IP2REGION_DB_FILE_PATH = "ip2region.xdb";
private static final String UNKNOW_IP = "未知IP";
private static final Searcher searcher = initSearcher();
private static Searcher initSearcher() {
try (InputStream is = new ClassPathResource(IP2REGION_DB_FILE_PATH).getInputStream();) {
byte[] cBuff = StreamUtils.copyToByteArray(is);// Searcher.loadContent(randomAccessFile);
return Searcher.newWithBuffer(cBuff);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getIpLocation(String ip) {
return "中国";
try {
return searcher.search(ip).split("\\|")[2].replaceFirst("省", "");
} catch (Exception e) {
return UNKNOW_IP;
}
}
}

@ -0,0 +1,15 @@
package com.luoo.user.util;
import static org.junit.Assert.*;
import org.junit.Test;
public class IpUtilTest {
@Test
public void test() {
String ip="121.8.215.106";
System.out.println(IpUtil.getIpLocation(ip));
}
}
Loading…
Cancel
Save