当前位置:网站首页>IP tools
IP tools
2022-07-07 19:39:00 【Scarecrow 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;
}
}
}
边栏推荐
- Numpy——2. Shape of array
- R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
- 一锅乱炖,npm、yarn cnpm常用命令合集
- L1-027 rental (Lua)
- 解决rosdep的报错问题
- Research and practice of super-resolution technology in the field of real-time audio and video
- 转置卷积理论解释(输入输出大小分析)
- Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers
- “本真”是什么意思
- Zhong Xuegao wants to remain innocent in the world
猜你喜欢
ES6 note 1
2022.07.05
8 CAS
State mode - Unity (finite state machine)
Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers
Kirin Xin'an with heterogeneous integration cloud financial information and innovation solutions appeared at the 15th Hunan Financial Technology Exchange Conference
博睿数据入选《2022爱分析 · IT运维厂商全景报告》
ASP.NET幼儿园连锁管理系统源码
Numpy——axis
cmd命令进入MySQL时报服务名或者命令错误(傻瓜式教学)
随机推荐
L1-023 output gplt (Lua)
怎么在手机上买股票开户 股票开户安全吗
# 欢迎使用Markdown编辑器
Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)
Teach your sister to write the message queue hand in hand
多个kubernetes集群如何实现共享同一个存储
LeetCode 648(C#)
2022年投资哪个理财产品收益高?
Responsibility chain model - unity
2022.07.05
How to share the same storage among multiple kubernetes clusters
Initial experience of cache and ehcache "suggestions collection"
Command mode - unity
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
MySQL、sqlserver oracle数据库连接方式
Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog
POJ 2392 Space Elevator
ASP.NET体育馆综合会员管理系统源码,免费分享
项目经理『面试八问』,看了等于会了