/*****************************************************************************
* 点分十进制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();
}

最新文章

  1. 关于C3翘边阴影的demo
  2. python调用zabbix接口实现Action配置
  3. 解决.NET Core中MailKit无法使用阿里云邮件推送服务的问题
  4. 解决MVC中使用BundleConfig.RegisterBundles引用Css及js文件发布后丢失的问题
  5. 使用Ganglia监控hadoop、hbase
  6. javascript对象(1)
  7. Aeroplane chess(HDU 4405)
  8. Spring实战3:装配bean的进阶知识
  9. 神奇的layout_weight属性
  10. 前端开发【第6篇:JavaScript客户端(浏览器)】
  11. 【oneday_onepage】——Growth Is A Bitch
  12. php能干什么?
  13. 玩转X-CTR100 | STM32F4 l GPIO位带操作
  14. 用switch函数根据选择不同的radio出现不同的视图
  15. mono for android之文件系统与应用程序首选项(转)
  16. 当网页失去焦点时改变网页的title值
  17. Android之MessageQueue、Looper、Handler与消息循环
  18. Yahoo关于性能优化的N条规则
  19. Vscode配置C++运行环境(2019/1//11更)并加入bits/stdc++.h头文件
  20. Layer弹出层关闭后刷新父页面

热门文章

  1. HDU 2843 I Will Win(概率题?,怨念颇深,简单)
  2. iOS-CALayer遮罩效果
  3. PHP 判断是否包含某字符串
  4. C#中毫米与像素的换算方法
  5. unity3d设置3D模型显示在2D背景之前(多个相机分层显示)(转)
  6. 检查和收集 Linux 硬件信息的 7 个命令
  7. 8 simple things that will make you sexy
  8. jquery.lazyload用法
  9. MVC新手指南
  10. Wireshark 网络抓包工具Wireshark的使用