|
@@ -5,11 +5,17 @@ import org.lionsoul.ip2region.DataBlock;
|
|
import org.lionsoul.ip2region.DbConfig;
|
|
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.DefaultResourceLoader;
|
|
import org.springframework.core.io.DefaultResourceLoader;
|
|
|
|
+import org.springframework.core.io.Resource;
|
|
import org.springframework.core.io.ResourceLoader;
|
|
import org.springframework.core.io.ResourceLoader;
|
|
import org.springframework.util.FileCopyUtils;
|
|
import org.springframework.util.FileCopyUtils;
|
|
|
|
|
|
|
|
+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;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -79,4 +85,89 @@ 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) {
|
|
|
|
+ System.out.println(getCityInfo1("192.168.3.117"));
|
|
|
|
+ }
|
|
}
|
|
}
|