parent
abffc680cc
commit
2f40ec8c2c
@ -1,7 +1,30 @@
|
|||||||
package com.luoo.user.util;
|
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 {
|
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) {
|
public static String getIpLocation(String ip) {
|
||||||
return "中国";
|
try {
|
||||||
|
return searcher.search(ip).split("\\|")[2].replaceFirst("省", "");
|
||||||
|
} catch (Exception e) {
|
||||||
|
return UNKNOW_IP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
@ -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…
Reference in new issue