当前位置:网站首页>IP 工具类
IP 工具类
2022-07-07 17:29:00 【稻草人0.0】
public class IPUtils {
public IPUtils() {
}
public static String convertIPToString(long ipLong) {
if (ipLong < 0L) {
ipLong += 4294967296L;
}
StringBuilder builder = new StringBuilder(15);
builder.append(ipLong >> 24);
builder.append('.');
builder.append((ipLong & 16777215L) >> 16);
builder.append('.');
builder.append((ipLong & 65535L) >> 8);
builder.append('.');
builder.append(ipLong & 255L);
return builder.toString();
}
public static Long convertIPToLong(String ipAddr, Long defaultValue) {
try {
return convertIPToLong(ipAddr);
} catch (IllegalArgumentException var3) {
return defaultValue;
}
}
public static long convertIPToLong(String ipAddr) throws IllegalArgumentException {
if (ipAddr != null && isIPAddr(ipAddr)) {
String[] ips = ipAddr.split("\\.");
long ipLong = 0L;
ipLong += Long.parseLong(ips[0]) << 24;
ipLong += Long.parseLong(ips[1]) << 16;
ipLong += Long.parseLong(ips[2]) << 8;
ipLong += Long.parseLong(ips[3]);
return ipLong;
} else {
throw new IllegalArgumentException("`" + ipAddr + "` is not a valid IP address!");
}
}
public static Integer convertIPToInt(String ipAddr, Integer defaultValue) {
try {
return convertIPToInt(ipAddr);
} catch (IllegalArgumentException var3) {
return defaultValue;
}
}
public static int convertIPToInt(String ipAddr) {
long ipVal = convertIPToLong(ipAddr);
if (ipVal > 2147483647L) {
ipVal -= 4294967296L;
}
return Long.valueOf(ipVal).intValue();
}
public static boolean isIPAddr(String addr) {
if (addr != null && addr.trim().length() != 0) {
String[] ips = addr.split("\\.");
if (ips.length != 4) {
return false;
} else {
try {
int ipa = Integer.parseInt(ips[0]);
int ipb = Integer.parseInt(ips[1]);
int ipc = Integer.parseInt(ips[2]);
int ipd = Integer.parseInt(ips[3]);
return ipa >= 0 && ipa <= 255 && ipb >= 0 && ipb <= 255 && ipc >= 0 && ipc <= 255 && ipd >= 0 && ipd <= 255;
} catch (NumberFormatException var6) {
return false;
}
}
} else {
return false;
}
}
}
边栏推荐
- 2022.07.05
- Install mysql8 for Linux X ultra detailed graphic tutorial
- ES6笔记一
- AI写首诗
- Jürgen Schmidhuber回顾LSTM论文等发表25周年:Long Short-Term Memory. All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversarial RL. Soccer learn
- Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
- 5billion, another master fund was born in Fujian
- Big Ben (Lua)
- R语言dplyr包select函数、group_by函数、filter函数和do函数获取dataframe中指定因子变量中指定水平中特定数值数据列的值第三大的值
- How much does it cost to develop a small program mall?
猜你喜欢
Kirin Xin'an with heterogeneous integration cloud financial information and innovation solutions appeared at the 15th Hunan Financial Technology Exchange Conference
Jerry's headphones with the same channel are not allowed to pair [article]
Business experience in virtual digital human
爬虫实战(七):爬王者英雄图片
2022.07.02
Kirin Xin'an joins Ningxia commercial cipher Association
小试牛刀之NunJucks模板引擎
How to estimate the value of "not selling pens" Chenguang?
杰理之关于 TWS 声道配置【篇】
SlashData开发者工具榜首等你而定!!!
随机推荐
歌单11111
Flipping game (enumeration)
How many are there (Lua)
网易云信参与中国信通院《实时音视频服务(RTC)基础能力要求及评估方法》标准编制...
what‘s the meaning of inference
2022.07.04
# 欢迎使用Markdown编辑器
ASP.NET幼儿园连锁管理系统源码
Numpy——axis
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Review of network attack and defense
超分辨率技术在实时音视频领域的研究与实践
R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
Zhong Xuegao wants to remain innocent in the world
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
指定opencv非标准安装的版本
杰理之快速配对,不支持取消配对【篇】
LeetCode 648(C#)
LeetCode 497(C#)
Kirin Xin'an joins Ningxia commercial cipher Association