|
@@ -6,16 +6,11 @@ import org.lionsoul.ip2region.DbConfig;
|
|
import org.lionsoul.ip2region.DbSearcher;
|
|
import org.lionsoul.ip2region.DbSearcher;
|
|
import org.lionsoul.ip2region.Util;
|
|
import org.lionsoul.ip2region.Util;
|
|
import org.springframework.core.io.ClassPathResource;
|
|
import org.springframework.core.io.ClassPathResource;
|
|
-import org.springframework.core.io.DefaultResourceLoader;
|
|
|
|
import org.springframework.core.io.Resource;
|
|
import org.springframework.core.io.Resource;
|
|
-import org.springframework.core.io.ResourceLoader;
|
|
|
|
import org.springframework.util.FileCopyUtils;
|
|
import org.springframework.util.FileCopyUtils;
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
-import java.io.FileOutputStream;
|
|
|
|
-import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
-import java.io.OutputStream;
|
|
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -25,20 +20,44 @@ import java.lang.reflect.Method;
|
|
*/
|
|
*/
|
|
public class IPUtil {
|
|
public class IPUtil {
|
|
|
|
|
|
- //国家,区域,省份,城市,运营商
|
|
|
|
- public static String getCityInfo(String ip) {
|
|
|
|
|
|
+ private static DbSearcher searcher;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取DbSearcher
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static DbSearcher getInstance () {
|
|
|
|
+ if (searcher != null) {
|
|
|
|
+ return searcher;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ DbConfig config = new DbConfig();
|
|
|
|
+ File file = new File("/ip2region.db"); // 本路获取资源文件
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
+ Resource resource = new ClassPathResource("/ip2region.db"); // 远程获取资源文件
|
|
|
|
+ InputStream inputStream = resource.getInputStream();
|
|
|
|
+ searcher = new DbSearcher(config, FileCopyUtils.copyToByteArray(inputStream));
|
|
|
|
+ } else {
|
|
|
|
+ searcher = new DbSearcher(config, file.getPath());
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ System.out.println("获取DbSearcher错误");
|
|
|
|
+ }
|
|
|
|
+ return searcher;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //国家,区域,省份,城市,运营商
|
|
|
|
+ public static String getCityInfo(String ip) {
|
|
//查询算法
|
|
//查询算法
|
|
- int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
|
|
|
|
|
|
+ int algorithm = DbSearcher.MEMORY_ALGORITYM;
|
|
|
|
+ //DbSearcher.BTREE_ALGORITHM; //B-tree
|
|
//DbSearcher.BINARY_ALGORITHM //Binary
|
|
//DbSearcher.BINARY_ALGORITHM //Binary
|
|
//DbSearcher.MEMORY_ALGORITYM //Memory
|
|
//DbSearcher.MEMORY_ALGORITYM //Memory
|
|
try {
|
|
try {
|
|
- //db
|
|
|
|
- ResourceLoader resourceLoader = new DefaultResourceLoader();
|
|
|
|
- InputStream stream = resourceLoader.getResource("classpath:ip2region.db").getInputStream();
|
|
|
|
-
|
|
|
|
- DbConfig config = new DbConfig();
|
|
|
|
- DbSearcher searcher = new DbSearcher(config, FileCopyUtils.copyToByteArray(stream));
|
|
|
|
|
|
+ // 初始化
|
|
|
|
+ getInstance();
|
|
|
|
|
|
//define the method
|
|
//define the method
|
|
Method method = null;
|
|
Method method = null;
|
|
@@ -65,9 +84,9 @@ public class IPUtil {
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
|
+ System.out.println("Ip解析错误");
|
|
}
|
|
}
|
|
-
|
|
|
|
- return "0|0|0|内网IP|内网IP";
|
|
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -86,88 +105,8 @@ public class IPUtil {
|
|
return new IpEntity();
|
|
return new IpEntity();
|
|
}
|
|
}
|
|
|
|
|
|
- public static void inputstreamtofile(InputStream ins, File file) {
|
|
|
|
- try {
|
|
|
|
- OutputStream os = new FileOutputStream(file);
|
|
|
|
- int bytesRead = 0;
|
|
|
|
- byte[] buffer = new byte[8192];
|
|
|
|
- while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
|
|
|
|
- os.write(buffer, 0, bytesRead);
|
|
|
|
- }
|
|
|
|
- os.close();
|
|
|
|
- ins.close();
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- //国家,区域,省份,城市,运营商
|
|
|
|
- public static String getCityInfo1(String ip) {
|
|
|
|
-
|
|
|
|
- //db
|
|
|
|
- String dbPath = "";
|
|
|
|
- try {
|
|
|
|
-// File file = ResourceUtils.getFile("classpath:ip2region.db");
|
|
|
|
-
|
|
|
|
- File file = new File("/ip2region.db");//临时图片存在的位置
|
|
|
|
- if (!file.exists()) {
|
|
|
|
- Resource resource = new ClassPathResource("/ip2region.db");
|
|
|
|
- try {
|
|
|
|
- InputStream inputStream = resource.getInputStream();
|
|
|
|
- inputstreamtofile(inputStream, file);
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
-// InputStream in = this.getClass().getResourceAsStream("/ip2region.db");//图片在项目中的位置
|
|
|
|
-// FileUtil.inputstreamtofile(in, f);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- dbPath = file.getPath();
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- System.out.println("文件读取错误");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //查询算法
|
|
|
|
- int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
|
|
|
|
- //DbSearcher.BINARY_ALGORITHM //Binary
|
|
|
|
- //DbSearcher.MEMORY_ALGORITYM //Memory
|
|
|
|
- try {
|
|
|
|
- DbConfig config = new DbConfig();
|
|
|
|
- DbSearcher searcher = new DbSearcher(config, dbPath);
|
|
|
|
-
|
|
|
|
- //define the method
|
|
|
|
- Method method = null;
|
|
|
|
- switch (algorithm) {
|
|
|
|
- case DbSearcher.BTREE_ALGORITHM:
|
|
|
|
- method = searcher.getClass().getMethod("btreeSearch", String.class);
|
|
|
|
- break;
|
|
|
|
- case DbSearcher.BINARY_ALGORITHM:
|
|
|
|
- method = searcher.getClass().getMethod("binarySearch", String.class);
|
|
|
|
- break;
|
|
|
|
- case DbSearcher.MEMORY_ALGORITYM:
|
|
|
|
- method = searcher.getClass().getMethod("memorySearch", String.class);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- DataBlock dataBlock = null;
|
|
|
|
- if (Util.isIpAddress(ip) == false) {
|
|
|
|
- System.out.println("Error: Invalid ip address");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- dataBlock = (DataBlock) method.invoke(searcher, ip);
|
|
|
|
-
|
|
|
|
- return dataBlock.getRegion();
|
|
|
|
-
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- System.out.println("Ip解析错误");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
- System.out.println(getCityInfo1("192.168.3.117"));
|
|
|
|
|
|
+ System.out.println(getCityInfo("223.93.170.82"));
|
|
|
|
+ System.out.println(getCityInfo("223.93.170.82"));
|
|
}
|
|
}
|
|
}
|
|
}
|