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,一步步断点跟 ...
随机推荐
- 防微杜渐——读《C安全编码标准》
防微杜渐——读<C安全编码标准> 首先这本书的名字是非常洋气的——<C安全编码标准>.然而映入眼帘的却不仅仅是冷冰冰的编码,还有那一套非常严谨的风险评估标准和问题处理方法.对于 ...
- 反射式DLL注入--方法
使用RWX权限打开目标进程,并为该DLL分配足够大的内存. 将DLL复制到分配的内存空间. 计算DLL中用于执行反射加载的导出的内存偏移量. 调用CreateRemoteThread(或类似的未公开的 ...
- 如何监视 Azure 中的虚拟机
通过收集.查看和分析诊断与日志数据,可以利用很多机会来监视 VM. 若要执行简单的 VM 监视,可以在 Azure 门户中使用 VM 的“概述”屏幕. 可以使用扩展在 VM 上配置诊断以收集更多指标数 ...
- NodeJS链接MySql数据库
//1.用npm命令安装mysql模块 npm install mysql //2.js文件中引入mysql模块 const mysqlModule = require('mysql'); //3.创 ...
- excel表格中添加单引号的方法
今天碰到需要插入大量数据的excel表格,其中有很多文本,需要添加单引号. 方法如下: 左边是原始数据,右边是我即将添加单引号的空白区域. 第一步:在需要添加的位置输入= 第二步:输入等号之后点击需要 ...
- Linux系统之TroubleShooting(启动故障排除)
尽管Linux系统非常强大,稳定,但是我们在使用过程当中,如果人为操作不当,仍然会影响系统,甚至可能使得系统无法开机,无法运行服务等等各种问题.那么这篇博文就总结一下一些常见的故障排除方法,但是不可能 ...
- sudo控制权限简单用法介绍
为了安全及管理的方便,可将需要用root权限的用户加入到sudo管理,用root的权限来管理系统.利用sudo控制用户对系统命令的使用权限. 普通用户可以查看,但不能删除: 但是在/tmp公共环境下可 ...
- [Spark Core] Spark 实现气温统计
0. 说明 聚合气温数据,聚合出 MAX . MIN . AVG 1. Spark Shell 实现 1.1 MAX 分步实现 # 加载文档 val rdd1 = sc.textFile(" ...
- security/pam_appl.h:没有那个文件或目录
在编译开源库时, 提示 pam.h:4:10: 致命错误:security/pam_appl.h:没有那个文件或目录 #include <security/pam_appl.h> 解决方法 ...
- windows Server 2008R2 FTP服务器搭建详细图解
一.安装ftp服务 1.打开服务器管理器,如图: 2.右键点击角色,如图: 3.点击添加角色,会出现添加角色向导对话框,如图: 4.点击下一步,选择要添加的“web服务器(IIS)” ‘’ 5.点击下 ...