当前位置:网站首页>根据IP获取地市
根据IP获取地市
2022-07-07 02:29:00 【Q z1997】
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
/** * 根据IP获取地址信息 * * @author zhangqi * @date 2022/7/3 20:32 */
@SuppressWarnings("unused")
@Slf4j
public class AddressUtils {
/** * IP地址查询 */
private static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
/** * 未知地址 */
private static final String UNKNOWN = "XX XX";
public static String getRealAddressByIP(String ip) {
// 内网不查询
if (internalIp(ip)) {
return "内网IP";
}
try {
HttpResponse httpResponse = HttpUtils.get(IP_URL + "ip=" + ip + "&json=true", null, null, CharsetUtil.CHARSET_GBK);
if (!httpResponse.isOk()) {
return UNKNOWN;
}
String body = httpResponse.body();
if (StringUtils.isEmpty(body)) {
return UNKNOWN;
}
JSONObject obj = JSON.parseObject(body);
String region = obj.getString("pro");
String city = obj.getString("city");
return String.format("%s %s", region, city);
} catch (Exception e) {
log.error("获取地理位置异常 {}", ip);
}
return UNKNOWN;
}
/** * 检查是否为内部IP地址 * * @param ip IP地址 * @return 结果 */
public static boolean internalIp(String ip) {
byte[] addr = textToNumericFormatV4(ip);
return internalIp(addr) || "127.0.0.1".equals(ip);
}
/** * 检查是否为内部IP地址 * * @param addr byte地址 * @return 结果 */
private static boolean internalIp(byte[] addr) {
if (ArrayUtils.isEmpty(addr) || addr.length < 2) {
return true;
}
final byte b0 = addr[0];
final byte b1 = addr[1];
// 10.x.x.x/8
final byte SECTION_1 = 0x0A;
// 172.16.x.x/12
final byte SECTION_2 = (byte) 0xAC;
final byte SECTION_3 = (byte) 0x10;
final byte SECTION_4 = (byte) 0x1F;
// 192.168.x.x/16
final byte SECTION_5 = (byte) 0xC0;
final byte SECTION_6 = (byte) 0xA8;
switch (b0) {
case SECTION_1:
return true;
case SECTION_2:
return (b1 >= SECTION_3 && b1 <= SECTION_4);
case SECTION_5:
return (b1 == SECTION_6);
default:
return false;
}
}
/** * 将IPv4地址转换成字节 * * @param text IPv4地址 * @return byte 字节 */
private static byte[] textToNumericFormatV4(String text) {
if (text.length() == 0) {
return new byte[0];
}
byte[] bytes = new byte[4];
String[] elements = text.split("\\.", -1);
try {
long l;
int i;
switch (elements.length) {
case 1:
l = Long.parseLong(elements[0]);
if ((l < 0L) || (l > 4294967295L)) {
return new byte[0];
}
bytes[0] = (byte) (int) (l >> 24 & 0xFF);
bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 2:
l = Integer.parseInt(elements[0]);
if ((l < 0L) || (l > 255L)) {
return new byte[0];
}
bytes[0] = (byte) (int) (l & 0xFF);
l = Integer.parseInt(elements[1]);
if ((l < 0L) || (l > 16777215L)) {
return new byte[0];
}
bytes[1] = (byte) (int) (l >> 16 & 0xFF);
bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 3:
for (i = 0; i < 2; ++i) {
l = Integer.parseInt(elements[i]);
if ((l < 0L) || (l > 255L)) {
return new byte[0];
}
bytes[i] = (byte) (int) (l & 0xFF);
}
l = Integer.parseInt(elements[2]);
if ((l < 0L) || (l > 65535L)) {
return new byte[0];
}
bytes[2] = (byte) (int) (l >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 4:
for (i = 0; i < 4; ++i) {
l = Integer.parseInt(elements[i]);
if ((l < 0L) || (l > 255L)) {
return new byte[0];
}
bytes[i] = (byte) (int) (l & 0xFF);
}
break;
default:
return new byte[0];
}
} catch (NumberFormatException e) {
return new byte[0];
}
return bytes;
}
}
边栏推荐
- Redhat5 installing vmware tools under virtual machine
- docker-compose启动redis集群
- "Parse" focalloss to solve the problem of data imbalance
- [start from scratch] detailed process of deploying yolov5 in win10 system (CPU, no GPU)
- mobx 知识点集合案例(快速入门)
- 请问如何查一篇外文文献的DOI号?
- 偏执的非合格公司
- Redis (I) -- getting to know redis for the first time
- FlexRay通信协议概述
- 精准时空行程流调系统—基于UWB超高精度定位系统
猜你喜欢

如何给目标机器人建模并仿真【数学/控制意义】

程序员的日常 | 每日趣闻

一段程序让你明白什么静态内部类,局部内部类,匿名内部类

Which foreign language periodicals are famous in geology?

品牌电商如何逆势增长?在这里预见未来!

Developers don't miss it! Oar hacker marathon phase III chain oar track registration opens

Linear algebra (1)

毕业设计游戏商城

【OpenCV】形态学滤波(2):开运算、形态学梯度、顶帽、黑帽

Redhat5 installing vmware tools under virtual machine
随机推荐
js装饰器@decorator学习笔记
品牌电商如何逆势增长?在这里预见未来!
快速定量,Abbkine 蛋白质定量试剂盒BCA法来了!
Stack and queue-p79-9
微信小程序隐藏video标签的进度条组件
一条慢SQL拖死整个系统
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
ICML 2022 | explore the best architecture and training method of language model
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!
Wechat applet hides the progress bar component of the video tag
How to find the literature of a foreign language journal?
Common problems of caching in high concurrency scenarios
Redis (II) - redis General Command
企业如何进行数据治理?分享数据治理4个方面的经验总结
Go straight to the 2022ecdc fluorite cloud Developer Conference: work with thousands of industries to accelerate intelligent upgrading
Handling hardfault in RT thread
Learning notes | data Xiaobai uses dataease to make a large data screen
MySQL (x)
String (explanation)
Array proof during st table preprocessing