注意:之前我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 192.168.1.2 192.168.1.3"; String result = null; List<String> strList = new ArrayList<>(); if (ip.contains(",") && !ip.contains(";") && !ip.contains(" ") && !ip.contains(" ")) { String[] strings = ip.split(","); for (String str : strings) { strList.add(str); } } else if (ip.contains(";") && !ip.contains(",") && !ip.contains(" ") && !ip.contains(" ")) { String[] strings = ip.split(";"); for (String str : strings) { strList.add(str); } } else if (ip.contains(" ") || 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(" ") && !ip.contains(" ")) { String[] strings = ip.split(","); for (String str : strings) { strList.add(str); } } else if (ip.contains(";") && !ip.contains(",") && !ip.contains(" ") && !ip.contains(" ")) { String[] strings = ip.split(";"); for (String str : strings) { strList.add(str); } } else if (ip.contains(" ") || 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(" ") && !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(" ") && !ip.contains(" ")) { String[] strings = ip.split(","); for (String str : strings) { strList.add(str); } } else if (ip.contains(";") && !ip.contains(",") && !ip.contains(" ") && !ip.contains(" ")) { String[] strings = ip.split(";"); for (String str : strings) { strList.add(str); } } else if (ip.contains(" ") || 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(" ") && !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(" ") && !ip.contains(" ")) { strList.add(ip); } else if (!ip.contains("-") && !ip.contains(",") && !ip.contains(";") && !ip.contains(" ") && !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); }