IP 解析器(IpParser) test 和 生产环境 实现
注意:之前我maven居然没有引入 StringUtils 的包,然后引入了一个路径类似,但其实包路径不一样的 StringUtils ,居然是划掉的状态,像这样 StringUtils ,这个其实不是我要用的,所以maven检查一下有没有引入就行了
注意2:如果要用list包着map,那么在某些循环的情况下,map需要清空,不然会产生重复数据
这里 RM 要 put into lib 一下,不然web环境下跑 StringUtils 会报错(原来我以为这个包只会影响到项目部署的war,没想到居然还会影响到 IDE 的web运行环境)

test文件
import org.apache.commons.lang.StringUtils; import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern; public class IpParser { public static void main(String args[]) {
String ip = "192.168.1.1\n192.168.1.2\n192.168.1.3";
String result = null; List<String> strList = new ArrayList<>(); if (ip.contains(",") && !ip.contains(";") && !ip.contains("\n") && !ip.contains(" ")) {
String[] strings = ip.split(",");
for (String str :
strings) {
strList.add(str);
}
} else if (ip.contains(";") && !ip.contains(",") && !ip.contains("\n") && !ip.contains(" ")) {
String[] strings = ip.split(";");
for (String str :
strings) {
strList.add(str);
}
} else if (ip.contains("\n") || ip.contains(" ")) {
Scanner scanner = new Scanner(ip);
while (scanner.hasNext()) {
strList.add(scanner.next());
}
} else {
System.out.println("请输入正确且统一的分隔符");
System.exit(-1);
} for (String strlt :
strList) { String patternS = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))";
String patternMinus = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\-(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))";
String pattern24 = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}[1]\\/[2][4]"; boolean isMatchS = Pattern.matches(patternS, strlt);
boolean isMatchMinus = Pattern.matches(patternMinus, strlt);
boolean isMatch24 = Pattern.matches(pattern24, strlt); if (isMatchS == true) {
result = "S";
// System.out.println("单ip");
} else if (isMatchMinus == true) {
result = "Minus";
// System.out.println("-");
} else if (isMatch24 == true) {
result = "24";
// System.out.println("1/24");
} else {
System.out.println("格式不正确,请重新输入:");
} if ("S".equals(result)) {
System.out.println(strlt);
} else if ("Minus".equals(result)) {
String ipPrefix = StringUtils.substringBeforeLast(strlt, ".");
String ipSuffix = StringUtils.substringAfterLast(strlt, ".");
Integer prefix = Integer.valueOf(StringUtils.substringBefore(ipSuffix, "-"));
Integer suffix = Integer.valueOf(StringUtils.substringAfter(ipSuffix, "-"));
List<String> list = new ArrayList<>();
for (int i = prefix; i <= suffix; i++) {
list.add(ipPrefix + "." + i);
}
for (String lt : list
) {
System.out.println(lt);
} } else if ("24".equals(result)) {
}
}
System.out.println("exit::"); }
}
生产环境文件:
注意在循环里使用map的时候,某些场景下需要重新实例化map,否则会产生重复数据
@RequestMapping(value = "controller/json/AssetsController/newTask/fetchIpList")
@ResponseBody
public BaseResult fetchIpList(String ipgroup) { String ip = ipgroup;
String result = null; List<String> strList = new ArrayList<>();
List resultList = new ArrayList<>();
Map<String, String> map = new HashMap<>(); if (ip.contains(",") && !ip.contains(";") && !ip.contains("\n") && !ip.contains(" ")) {
String[] strings = ip.split(",");
for (String str :
strings) {
strList.add(str);
}
} else if (ip.contains(";") && !ip.contains(",") && !ip.contains("\n") && !ip.contains(" ")) {
String[] strings = ip.split(";");
for (String str :
strings) {
strList.add(str);
}
} else if (ip.contains("\n") || ip.contains(" ")) {
Scanner scanner = new Scanner(ip);
while (scanner.hasNext()) {
strList.add(scanner.next());
}
} else if (ip != null && ip != "" && !ip.contains(",") && !ip.contains(";") && !ip.contains("\n") && !ip.contains(" ")) {
resultList.add(ip);
return ResultUtil.success().add("data", resultList);
} else {
return ResultUtil.error("请输入正确且统一的分隔符");
} for (String strlt :
strList) { String patternS = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))";
String patternMinus = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\-(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))";
String pattern24 = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}[1]\\/[2][4]"; boolean isMatchS = Pattern.matches(patternS, strlt);
boolean isMatchMinus = Pattern.matches(patternMinus, strlt);
boolean isMatch24 = Pattern.matches(pattern24, strlt); if (isMatchS == true) {
result = "S";
} else if (isMatchMinus == true) {
result = "Minus";
} else if (isMatch24 == true) {
result = "24";
} else {
ResultUtil.error("ip格式不正确,请重新输入");
} if ("S".equals(result)) {
map.put("ip", strlt);
resultList.add(map);
map = new HashMap<>();
// 这里要用map实例化重置一下,不然会产生冗余数据
} else if ("Minus".equals(result)) {
String ipPrefix = StringUtils.substringBeforeLast(strlt, ".");
String ipSuffix = StringUtils.substringAfterLast(strlt, ".");
Integer prefix = Integer.valueOf(StringUtils.substringBefore(ipSuffix, "-"));
Integer suffix = Integer.valueOf(StringUtils.substringAfter(ipSuffix, "-"));
for (int i = prefix; i <= suffix; i++) {
map.put("ip", ipPrefix + "." + i);
resultList.add(map);
map = new HashMap<>();
// 这里要用map实例化重置一下,不然会产生冗余数据
}
} else if ("24".equals(result)) {
String ipPrefix = StringUtils.substringBeforeLast(strlt, ".");
for (int i = 1; i <= 254; i++) {
map.put("ip", ipPrefix + "." + i);
resultList.add(map);
map = new HashMap<>();
// 这里要用map实例化重置一下,不然会产生冗余数据
}
}
map = new HashMap<>();
// 这里要用map实例化重置一下,不然会产生冗余数据
}
return ResultUtil.success().add("data", resultList);
}
更新修改(对单个ip如 192.168.1.1 | 192.168.1.1-10 | 192.168.1.1/24 这样的情况进行了处理):
@RequestMapping(value = "controller/json/AssetsController/newTask/fetchIpList")
@ResponseBody
public BaseResult fetchIpList(String ipgroup) { String ip = ipgroup;
String result = null; List<String> strList = new ArrayList<>();
List resultList = new ArrayList<>();
Map<String, String> map = new HashMap<>(); if (ip == null || ip == "") {
return ResultUtil.error("请填入内容");
} else if (ip.contains(",") && !ip.contains(";") && !ip.contains("\n") && !ip.contains(" ")) {
String[] strings = ip.split(",");
for (String str :
strings) {
strList.add(str);
}
} else if (ip.contains(";") && !ip.contains(",") && !ip.contains("\n") && !ip.contains(" ")) {
String[] strings = ip.split(";");
for (String str :
strings) {
strList.add(str);
}
} else if (ip.contains("\n") || ip.contains(" ")) {
Scanner scanner = new Scanner(ip);
while (scanner.hasNext()) {
strList.add(scanner.next());
}
} else if (!ip.contains("/") && !ip.contains("-") && !ip.contains(",") && !ip.contains(";") && !ip.contains("\n") && !ip.contains(" ")) {
map.put("ip", ip);
resultList.add(map);
return ResultUtil.success().add("data", resultList);
} else if (!ip.contains("/") && !ip.contains(",") && !ip.contains(";") && !ip.contains("\n") && !ip.contains(" ")) {
strList.add(ip);
} else if (!ip.contains("-") && !ip.contains(",") && !ip.contains(";") && !ip.contains("\n") && !ip.contains(" ")) {
strList.add(ip);
} else {
return ResultUtil.error("请输入正确且统一的分隔符");
} for (String strlt :
strList) { String patternS = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))";
String patternMinus = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\-(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))";
String pattern24 = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}[1]\\/[2][4]"; boolean isMatchS = Pattern.matches(patternS, strlt);
boolean isMatchMinus = Pattern.matches(patternMinus, strlt);
boolean isMatch24 = Pattern.matches(pattern24, strlt); if (isMatchS == true) {
result = "S";
} else if (isMatchMinus == true) {
result = "Minus";
} else if (isMatch24 == true) {
result = "24";
} else {
ResultUtil.error("ip格式不正确,请重新输入");
} if ("S".equals(result)) {
map.put("ip", strlt);
resultList.add(map);
map = new HashMap<>();
} else if ("Minus".equals(result)) {
String ipPrefix = StringUtils.substringBeforeLast(strlt, ".");
String ipSuffix = StringUtils.substringAfterLast(strlt, ".");
Integer prefix = Integer.valueOf(StringUtils.substringBefore(ipSuffix, "-"));
Integer suffix = Integer.valueOf(StringUtils.substringAfter(ipSuffix, "-"));
for (int i = prefix; i <= suffix; i++) {
map.put("ip", ipPrefix + "." + i);
resultList.add(map);
map = new HashMap<>();
}
} else if ("24".equals(result)) {
String ipPrefix = StringUtils.substringBeforeLast(strlt, ".");
for (int i = 1; i <= 254; i++) {
map.put("ip", ipPrefix + "." + i);
resultList.add(map);
map = new HashMap<>();
}
}
map = new HashMap<>();
}
return ResultUtil.success().add("data", resultList);
}
IP 解析器(IpParser) test 和 生产环境 实现的更多相关文章
- IP工具类-自己动手做个ip解析器
IP工具类-自己动手做个ip解析器 一.资料准备 导入依赖包:
- node npm --save,不同JS解析器的内置全局变量,PROMISE,CONST---ES6
npm --save 当你为你的模块安装一个依赖模块时,正常情况下你得先安装他们(在模块根目录下npm install module-name),然后连同版本号手动将他们添加到模块配置文件packa ...
- 关于生产环境改用G1垃圾收集器的思考
背景 由于我们的业务量非常大,响应延迟要求高.目前沿用的老的ParNew+CMS已经不能支撑业务的需求.平均一台机器在1个月内有1次秒级别的stop the world.对系统来说是个巨大的隐患.所以 ...
- SUSE12Sp3安装配置.net core 生产环境(1)-IP,DNS,网关,SSH,GIT
1.新增用户 sudo useradd 用户名 sudo passwd 用户名 这个时候会提示你输入密码,输入两次密码即可 2.静态 IP 设置 1.设置 IP 地址 sudo vi /etc/sys ...
- 使用gunicorn将django项目部署到生产环境的子目录下,在nginx后端获取客户真实IP地址
生产环境有时,并不是为了一个项目而存在的.毕竟,域名是比较稀有的. 今天遇到这个问题,解决了.作个记录. 并且,如果将django项目部署在Nginx后面,那如何获取用户真实的IP地址呢? 下面就来解 ...
- Tomcat生产环境应用
概要: Tomcat各核心组件认知 Tomcat server.xml 配置详解 Tomcat IO模型介绍 一.Tomcat各组件认知 Tomcat架构说明 Tomcat组件及关系详情介绍 Tomc ...
- 理解Docker(6):若干企业生产环境中的容器网络方案
本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...
- NanoProfiler - 适合生产环境的性能监控类库 之 基本功能篇
背景 NanoProfiler是一个EF Learning Labs出品的免费性能监控类库(即将开源).它的思想和使用方式类似于MiniProfiler的.但是,设计理念有较大差异. MiniProf ...
- .NET C#微信公众号开发远程断点调试(本地远程调试生产环境代码)
最近在做微信公众号开发,由于之前没有接触过,突然发现调试不方便,不方便进行断点跟踪调试.因为微信那边绑定的服务器地址必须是公网地址,但是还是想进行断点调试(毕竟这样太方便了,程序有Bug,一步步断点跟 ...
随机推荐
- Laravel安装教程
1.Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length() 报这个错是因为Apache/bin目录下 l ...
- 服务器重启可能会导致SQL Server中部分数据库变为single user mode
今天检查公司生产服务器的SQL Server数据库,惊讶的发现有三个生产数据库变为了single user mode.奇怪的是没有任何人和程序执行过SQL语句将这三个数据库设置为single user ...
- 转:ASP.NET前台代码绑定后台变量方法总结
经常会碰到在前台代码中要使用(或绑定)后台代码中变量值的问题.一般有<%= str%>和<%# str %>两种方式,这里简单总结一下.如有错误或异议之处,敬请各位指教. 一方 ...
- UNIX高级环境编程(16)文件系统 < 雨后 >
来点绿色放松一下眼睛吧 :) 文件系统是对文件和目录的组织集合. 一 设备文件 设备文件和系统的某个设备相对应. 设备驱动程序 处理设备的所有IO请求. 提供了一致的API接口,对应于系统调用的ope ...
- PHP 传值操作和传地址操作
PHP中有两种赋值操作 一种是传值操作 example: $a = 1; $b = $b; 这个就是传值 传地址是: example2: $a = 1; $b = &$a; 这两者有什么区别呢 ...
- 汇编语言debug命令与指令机器码
一.debug命令 二.标志信息 -r用这个指令,得到的信息右下角: NV UP EI PL NZ NA PO NC 这些符号代表的就是标志寄存器里常用标志位的值.这个是符号值对应表: 溢出标志OF( ...
- 【优质】React的学习资源
React的学习资源 github 地址: https://github.com/LeuisKen/react-collection https://github.com/reactnativecn/ ...
- 【Alpha 冲刺】 1/12
1. 任务明细及任务量 Alpha版本任务安排(非固化版本,视情况调整,若有遗漏,及时补充) 职务 姓名 预期负责的模块页面 模块页面/任务明细 难度系数(0~1)(根据UI/功能实现难度划分) 预计 ...
- BeanDefinition及其实现类
[转自 http://blog.csdn.net/u011179993 ] 目录(?)[+] 一. BeanDefinition及其实现类 BeanDefinition接口 这个接口描述bea ...
- input file 美化
<input type='file'>的默认外观实在难看,绝大多数情况都需要对其美化.找了很多资料,目前发现以下方式是最简单的美化方式. 1.将file input用label包裹起来,然 ...