点分十进制IP校验、转换,掩码校验
2024-10-14 08:14:06
/*****************************************************************************
* 点分十进制IP校验、转换,掩码校验
* 声明:
* 本文主要记录如何对IP、掩码进行转换、校验等相关内容,注意大小端的问题。
*
* 2016-5-5 深圳 南山平山村 曾剑锋
****************************************************************************/ 一、参考文档:
. java编程,如何检查一个给定的字符串IP是否合法?
http://www.oschina.net/question/994728_115374
. js验证IP及子网掩码的合法性有效性示例
http://www.bkjia.com/Javascript/763031.html 二、点分十进制IP转整形
static public int numericIPToInt(String numericIP) {
String [] ips = numericIP.split("\\.");
int ip = Integer.valueOf(ips[]) << |
Integer.valueOf(ips[]) << |
Integer.valueOf(ips[]) << |
Integer.valueOf(ips[]); return ip;
} static public String intToNumericIP(int ip) {
return (ip & 0xff) + "."
+ ((ip >> ) & 0xff) + "."
+ ((ip >> ) & 0xff)+ "."
+ ((ip >> ) & 0xff));
} 三、IP校验
public static boolean isIpv4(String ipAddress) {
String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher(ipAddress);
return matcher.matches();
} 四、netmask校验
public static boolean isNetmask(String netmask) {
String mask="^(254|252|248|240|224|192|128|0)\\.0\\.0\\.0" +
"|255\\.(254|252|248|240|224|192|128|0)\\.0\\.0" +
"|255\\.255\\.(254|252|248|240|224|192|128|0)\\.0" +
"|255\\.255\\.255\\.(254|252|248|240|224|192|128|0)$";
Pattern pattern = Pattern.compile(mask);
Matcher matcher = pattern.matcher(netmask);
return matcher.matches();
}
最新文章
- 关于C3翘边阴影的demo
- python调用zabbix接口实现Action配置
- 解决.NET Core中MailKit无法使用阿里云邮件推送服务的问题
- 解决MVC中使用BundleConfig.RegisterBundles引用Css及js文件发布后丢失的问题
- 使用Ganglia监控hadoop、hbase
- javascript对象(1)
- Aeroplane chess(HDU 4405)
- Spring实战3:装配bean的进阶知识
- 神奇的layout_weight属性
- 前端开发【第6篇:JavaScript客户端(浏览器)】
- 【oneday_onepage】——Growth Is A Bitch
- php能干什么?
- 玩转X-CTR100 | STM32F4 l GPIO位带操作
- 用switch函数根据选择不同的radio出现不同的视图
- mono for android之文件系统与应用程序首选项(转)
- 当网页失去焦点时改变网页的title值
- Android之MessageQueue、Looper、Handler与消息循环
- Yahoo关于性能优化的N条规则
- Vscode配置C++运行环境(2019/1//11更)并加入bits/stdc++.h头文件
- Layer弹出层关闭后刷新父页面