zoukankan      html  css  js  c++  java
  • 关于IP的工具类

    很多WEB应用有用到对IP的处理,最近刚好在做这个东西,顺便记录一下:

     1 import org.apache.commons.lang.StringUtils;
     2 
     3 /**
     4  * Ip工具类
     5  *
     6  */
     7 public class IpUtils {
     8 
     9     /**
    10      * 将IP地址格式化为定长的IP段.
    11      * 例如: 192.168.0.122 => 192.168.000.122
    12      * @param ip
    13      * @return
    14      */
    15     public static String formatIp(String ip) {
    16         String[] ips = ip.split("\.");
    17         String newIp = "";
    18         for (int i = 0; i < ips.length; i++) {
    19             switch (ips[i].length()) {
    20             case 1:
    21                 newIp += "00" + ips[i] + ".";
    22                 break;
    23             case 2:
    24                 newIp += "0" + ips[i] + ".";
    25                 break;
    26             case 3:
    27                 newIp += ips[i] + ".";
    28                 break;
    29             }
    30         }
    31         newIp = newIp.substring(0, newIp.length() - 1);
    32         return newIp;
    33     }
    34 
    35     /**
    36      * 将定长的IP段还原.
    37      * 例如: 010.001.000.122 => 10.1.0.122
    38      * @param ip
    39      * @return
    40      */
    41     public static String normalIp(String ip) {
    42         //转换ip字符串格式
    43         String[] ipArray = StringUtils.split(ip, ".");
    44         for(int i=0;i<ipArray.length;i++)
    45         {
    46             int temp = Integer.parseInt(ipArray[i]);
    47             ipArray[i] = String.valueOf(temp);
    48         }
    49         return StringUtils.join(ipArray,".");
    50     }
    51     
    52     /**
    53      * 把ip地址转换成数字
    54      * @param ip
    55      * @return
    56      */
    57     public static long ip2Long(String ip) {
    58         String[] ipArray = StringUtils.split(ip, ".");
    59         long retLong = 0;
    60         if (!checkIP(ip)) {
    61             return -1;
    62         }
    63         for(int i=0;i<ipArray.length;i++)
    64         {
    65             long temp = Long.parseLong(ipArray[i]);
    66             int j = 3 - i;
    67             while (j > 0) {
    68                 temp = temp * 256;
    69                 j--;
    70             }
    71             retLong += temp;
    72         }
    73         return retLong;
    74     }
    75     
    76     public static boolean checkIP(String ip){
    77         return ip.matches("\d{1,3}+\.\d{1,3}+\.\d{1,3}+\.\d{1,3}");
    78     }
    79 
    80     
    81     public static void main(String[] args) {
    82         System.out.println(IpUtils.ip2Long("255.255.255.255"));
    83         System.out.println(IpUtils.checkIP("19a2.1685.0.4"));
    84     }
    85 
    86 }
    ==================================
    I'm not just a coder
    keep running,keep fighting...
    ==================================
  • 相关阅读:
    .net面试--值类型和引用类型
    Centos7下安装Docker(详细的新手装逼教程)
    C# 开源框架(整理)
    service配置文件
    kafka消息队列、环境搭建与使用(.net framework)
    消息队列
    并发、并行、同步、异步、多线程的区别
    破解studio 3T
    HM后台(二)
    HM后台(一)
  • 原文地址:https://www.cnblogs.com/supergigi/p/3488645.html
Copyright © 2011-2022 走看看