当前位置:网站首页>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;
}
}
}
边栏推荐
- 爬虫实战(七):爬王者英雄图片
- Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)
- AI写首诗
- 杰理之手动配对方式【篇】
- Command mode - unity
- 杰理之开机自动配对【篇】
- R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化分组经验累积密度分布函数曲线、linetype参数指定不同分组曲线的线型
- how to prove compiler‘s correctness
- MySQL、sqlserver oracle数据库连接方式
- Jerry's headphones with the same channel are not allowed to pair [article]
猜你喜欢
杰理之手动配对方式【篇】
Matplotlib drawing 3D graphics
ES6笔记一
杰理之相同声道的耳机不允许配对【篇】
Business experience in virtual digital human
多个kubernetes集群如何实现共享同一个存储
位运算介绍
Redis master-slave and sentinel master-slave switchover are built step by step
杰理之关于 TWS 配对方式配置【篇】
Version 2.0 of tapdata, the open source live data platform, has been released
随机推荐
LeetCode 648(C#)
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
【RT-Thread env 工具安装】
2022如何评估与选择低代码开发平台?
杰理之关于 TWS 声道配置【篇】
【Confluence】JVM内存调整
2022年投资哪个理财产品收益高?
歌单11111
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
how to prove compiler‘s correctness
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
What does "true" mean
杰理之手动配对方式【篇】
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
小试牛刀之NunJucks模板引擎
解决rosdep的报错问题
2022.07.02
Unable to link the remote redis server (solution 100%
Kirin Xin'an with heterogeneous integration cloud financial information and innovation solutions appeared at the 15th Hunan Financial Technology Exchange Conference
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