mirror of
https://github.com/dqjdda/Aurora.git
synced 2018-11-06 05:33:29 +00:00
完善菜单管理,修改图标存储与显示方式,重置git仓库,更新.gitignore文件
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package me.aurora.util;
|
||||
|
||||
import org.lionsoul.ip2region.DataBlock;
|
||||
import org.lionsoul.ip2region.DbConfig;
|
||||
import org.lionsoul.ip2region.DbSearcher;
|
||||
import org.lionsoul.ip2region.Util;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/18 9:48:00
|
||||
*/
|
||||
public class AddressUtils {
|
||||
|
||||
static String region = "内网IP|0|0|内网IP|内网IP";
|
||||
|
||||
private AddressUtils() {
|
||||
}
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(AddressUtils.class);
|
||||
|
||||
public static String getCityInfo(String ip) {
|
||||
try {
|
||||
Resource resource = new ClassPathResource("ip2region/ip2region.db");
|
||||
File file = resource.getFile();
|
||||
if (!file.exists()) {
|
||||
log.error("Error: Invalid ip2region.db file");
|
||||
}
|
||||
int algorithm = DbSearcher.BTREE_ALGORITHM;
|
||||
DbConfig config = new DbConfig();
|
||||
DbSearcher searcher = new DbSearcher(config, file.getPath());
|
||||
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;
|
||||
default:
|
||||
method = searcher.getClass().getMethod("memorySearch", String.class);
|
||||
break;
|
||||
}
|
||||
DataBlock dataBlock = null;
|
||||
if (!Util.isIpAddress(ip)) {
|
||||
log.error("Error: Invalid ip address");
|
||||
}
|
||||
dataBlock = (DataBlock) method.invoke(searcher, ip);
|
||||
/**
|
||||
* 太难看了,转换下
|
||||
*/
|
||||
return dataBlock.getRegion().equals(region)?"内网IP":dataBlock.getRegion();
|
||||
} catch (Exception e) {
|
||||
log.error("获取地址信息异常:{}", e);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user