1. 判断某一IP地址是否在某一个IP段内

  1. //判断客户端ip是否在指定ip段范围内---与一个ip段比较
  2. public static boolean ipIsValid(String ipSection, String ip) {
  3. if (ipSection == null)
  4. throw new NullPointerException("IP段不能为空!");
  5. if (ip == null)
  6. throw new NullPointerException("IP不能为空!");
  7. ipSection = ipSection.trim();
  8. ip = ip.trim();
  9. final String REGX_IP = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)"; //合法的ip格式
  10. final String REGX_IPB = REGX_IP + "\\-" + REGX_IP;
  11. if (!ipSection.matches(REGX_IPB) || !ip.matches(REGX_IP)) //判断ip或者ip段是否合法
  12. return false;
  13. int idx = ipSection.indexOf('-');
  14. String[] sips = ipSection.substring(0, idx).split("\\.");
  15. String[] sipe = ipSection.substring(idx + 1).split("\\.");
  16. String[] sipt = ip.split("\\.");
  17. long ips = 0L, ipe = 0L, ipt = 0L;
  18. for (int i = 0; i < 4; ++i) {
  19. ips = ips << 8 | Integer.parseInt(sips[i]);
  20. ipe = ipe << 8 | Integer.parseInt(sipe[i]);
  21. ipt = ipt << 8 | Integer.parseInt(sipt[i]);
  22. }
  23. if (ips > ipe) {
  24. long t = ips;
  25. ips = ipe;
  26. ipe = t;
  27. }
  28. return ips <= ipt && ipt <= ipe;
  29. }

2. 判断某一IP地址是否在某几个IP段内

  1. //判断客户端ip是否在指定ip段范围内---与多个ip段(ipSections)比较
  2. public static boolean ipIsValids(String ipSections, String ip) {
  3. boolean flag=false;
  4. if (ipSections == null)
  5. throw new NullPointerException("IP段不能为空!");
  6. if (ip == null)
  7. throw new NullPointerException("IP不能为空!");
  8. ipSections = ipSections.trim();
  9. ip = ip.trim();
  10. String[] ipSection=ipSections.split(",");
  11. final String REGX_IP = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)"; //合法的ip格式
  12. final String REGX_IPB = REGX_IP + "\\-" + REGX_IP;
  13. if(!ip.matches(REGX_IP)){
  14. return flag;
  15. }
  16. String[] sipt = ip.split("\\.");
  17. for(int i=0;i
  18. if(!ipSection[i].matches(REGX_IPB) && !ipSection[i].matches(REGX_IP)) //判断ip或者ip段是否合法
  19. return flag;
  20. int idx = ipSection[i].indexOf('-');
  21. String[] sips = ipSection[i].substring(0, idx).split("\\.");
  22. String[] sipe = ipSection[i].substring(idx + 1).split("\\.");
  23. long ips = 0L, ipe = 0L, ipt = 0L;
  24. for (int j = 0; j < 4; ++j) {
  25. ips = ips << 8 | Integer.parseInt(sips[j]);
  26. ipe = ipe << 8 | Integer.parseInt(sipe[j]);
  27. ipt = ipt << 8 | Integer.parseInt(sipt[j]);
  28. }
  29. if (ips > ipe) {
  30. long t = ips;
  31. ips = ipe;
  32. ipe = t;
  33. }
  34. if(ips <= ipt && ipt <= ipe){
  35. flag=true;
  36. break;
  37. }else {
  38. continue;
  39. }
  40. }
  41. return flag;
  42. }

3. 判断某一IP地址是否在某一个或几个ip段或者等于制定的某个ip的范围内

  1. //判断客户端ip是否在指定ip段范围内---与多个ip段以及多个单一ip比较
  2. public static boolean ipIsValids(String ipSections, String ip) {
  3. boolean flag=false;
  4. if (ipSections == null)
  5. throw new NullPointerException("IP段不能为空!");
  6. if (ip == null)
  7. throw new NullPointerException("IP不能为空!");
  8. ipSections = ipSections.trim();
  9. ip = ip.trim();
  10. String[] ipSection=ipSections.split(",");
  11. final String REGX_IP = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)"; //合法的ip格式
  12. final String REGX_IPB = REGX_IP + "\\-" + REGX_IP;
  13. if(!ip.matches(REGX_IP)){
  14. return flag;
  15. }
  16. String[] sipt = ip.split("\\.");
  17. for(int i=0;i
  18. if(!ipSection[i].matches(REGX_IPB) && !ipSection[i].matches(REGX_IP)) //判断ip或者ip段是否合法
  19. return flag;
  20. if(ipSection[i].matches(REGX_IP)){
  21. flag=ip.equals(ipSection[i]);
  22. }else {
  23. int idx = ipSection[i].indexOf('-');
  24. String[] sips = ipSection[i].substring(0, idx).split("\\.");
  25. String[] sipe = ipSection[i].substring(idx + 1).split("\\.");
  26. long ips = 0L, ipe = 0L, ipt = 0L;
  27. for (int j = 0; j < 4; ++j) {
  28. ips = ips << 8 | Integer.parseInt(sips[j]);
  29. ipe = ipe << 8 | Integer.parseInt(sipe[j]);
  30. ipt = ipt << 8 | Integer.parseInt(sipt[j]);
  31. }
  32. if (ips > ipe) {
  33. long t = ips;
  34. ips = ipe;
  35. ipe = t;
  36. }
  37. if(ips <= ipt && ipt <= ipe){
  38. flag=true;
  39. break;
  40. }else {
  41. continue;
  42. }
  43. }
  44. }
  45. return flag;
  46. }

可参考文档:https://blog.csdn.net/whatzhang007/article/details/113601349#comments_18384389