当前位置:网站首页>Get the city according to IP
Get the city according to IP
2022-07-07 06:51: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;
/** * according to IP Get address information * * @author zhangqi * @date 2022/7/3 20:32 */
@SuppressWarnings("unused")
@Slf4j
public class AddressUtils {
/** * IP Address the query */
private static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
/** * Unknown address */
private static final String UNKNOWN = "XX XX";
public static String getRealAddressByIP(String ip) {
// Intranet does not query
if (internalIp(ip)) {
return " Intranet 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(" Get the location exception {}", ip);
}
return UNKNOWN;
}
/** * Check whether it is internal IP Address * * @param ip IP Address * @return result */
public static boolean internalIp(String ip) {
byte[] addr = textToNumericFormatV4(ip);
return internalIp(addr) || "127.0.0.1".equals(ip);
}
/** * Check whether it is internal IP Address * * @param addr byte Address * @return result */
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;
}
}
/** * take IPv4 Address to byte * * @param text IPv4 Address * @return byte 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;
}
}
边栏推荐
- Postgresql中procedure支持事务语法(实例&分析)
- 精准时空行程流调系统—基于UWB超高精度定位系统
- Abnova 体外转录 mRNA工作流程和加帽方法介绍
- SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
- MOS tube parameters μ A method of Cox
- mobx 知识点集合案例(快速入门)
- 2022 Android interview essential knowledge points, a comprehensive summary
- ViewModelProvider.of 过时方法解决
- Can't you really do it when you are 35 years old?
- Which foreign language periodicals are famous in geology?
猜你喜欢
品牌电商如何逆势增长?在这里预见未来!
Go straight to the 2022ecdc fluorite cloud Developer Conference: work with thousands of industries to accelerate intelligent upgrading
Config分布式配置中心
String (explanation)
Take you to brush (niuke.com) C language hundred questions (the first day)
剑指offer-高质量的代码
jdbc数据库连接池使用问题
循环肿瘤细胞——Abnova 解决方案来啦
MySQL的安装
Jmeter 5.5版本发布说明
随机推荐
【mysqld】Can't create/write to file
MySQL (x)
品牌电商如何逆势增长?在这里预见未来!
华为机试题素数伴侣
Learning notes | data Xiaobai uses dataease to make a large data screen
Redhat5 installing vmware tools under virtual machine
Etcd database source code analysis -- starting from the start function of raftnode
地质学类比较有名的外文期刊有哪些?
请问如何查一篇外文文献的DOI号?
MySQL installation
MySQL的安装
二十岁的我4面拿到字节跳动offer,至今不敢相信
String (explanation)
偏执的非合格公司
MySQL SQL的完整处理流程
7天零基础能考证HCIA吗?华为认证系统学习路线分享
怎样查找某个外文期刊的文献?
ViewModelProvider.of 过时方法解决
MySQL卸载文档-Windows版
A program lets you understand what static inner classes, local inner classes, and anonymous inner classes are