会员登录 - 用户注册 - 设为首页 - 加入收藏 - 网站地图 MySQL如何有效的存储IP地址?!

MySQL如何有效的存储IP地址?

时间:2025-11-04 23:22:12 来源:益强数据堂 作者:IT科技类资讯 阅读:550次
MySQL如何有效的存储IP地址?
复制package com.java.mmzsit;  /**   * @author :mmzsblog   * @description:Ipv4地址的有效转换   * @date :2020/5/27 22:43   */  public class Ipv4Covert {      public static void main(String[] args) {          String ip = "10.108.149.219";          // step1: 分解IP字符串,并对应写对字节数组          byte[] ip1 = ipToBytes(ip);          // step2: 对字节数组里的存P地每个字节进行左移位处理,分别对应到整型变量的有效4个字节          int ip2 = bytesToInt(ip1);          System.out.println("整型ip ----> " + ip2);          // step3: 对整型变量进行右位移处理,恢复IP字符串          String ip3 = intToIp(ip2);          System.out.println("字符串ip---->" + ip3);      }      /**       * 把IP地址转化为int       * @param ipAddr       * @return int       */      public static byte[] ipToBytesByReg(String ipAddr) {          byte[] ret = new byte[4];          try {              String[] ipArr = ipAddr.split(".");              ret[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF);              ret[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF);              ret[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF);              ret[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF);              return ret;          } catch (Exception e) {              throw new IllegalArgumentException(ipAddr + " is invalid IP");          }      }      /**       * 第一步,源码库存P地把IP地址分解为一个btye数组       */      public static byte[] ipToBytes(String ipAddr) {          // 初始化字节数组,有效定义长度为4          byte[] ret = new byte[4];          try {              String[] ipArr = ipAddr.split(".");              // 将字符串数组依次写入字节数组              ret[0] = (byte) (Integer.parseInt(ipArr[0]));              ret[1] = (byte) (Integer.parseInt(ipArr[1]));              ret[2] = (byte) (Integer.parseInt(ipArr[2]));              ret[3] = (byte) (Integer.parseInt(ipArr[3]));              return ret;          } catch (Exception e) {              throw new IllegalArgumentException("invalid IP : " + ipAddr);          }      }      /**       * 根据位运算把 byte[] -> int       * 原理:将每个字节强制转化为8位二进制码,存P地然后依次左移8位,有效对应到Int变量的存P地4个字节中       */      public static int bytesToInt(byte[] bytes) {          // 先移位后直接强转的同时指定位数          int addr = bytes[3] & 0xFF;          addr |= ((bytes[2] <<8) & 0xFF00);          addr |= ((bytes[1] <<16) & 0xFF0000);          addr |= ((bytes[0] <<24) & 0xFF000000);          return addr;      }      /**       * 把int->string地址       *       * @param ipInt       * @return String       */      public static String intToIp(int ipInt) {          // 先强转二进制,香港云服务器再进行移位处理          return new StringBuilder()                  // 右移3个字节(24位),有效得到IP地址的存P地第一段也就是byte[0],为了防止符号位是1也就是负数,免费源码下载有效最后再一次& 0xFF                  .append(((ipInt & 0xFF000000) >> 24) & 0xFF).append(.)                  .append((ipInt & 0xFF0000) >> 16).append(.)                  .append((ipInt & 0xFF00) >> 8).append(.)                  .append((ipInt & 0xFF))                  .toString();      }  }  1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.

(责任编辑:人工智能)

推荐内容
  • 飞利浦42pf7320(探索飞利浦42pf7320的品质、功能和性能)
  • 域名系统简称是什么?是用来干嘛的?
  • Java开发人员的十大测试框架和库
  • 查域名怎么查?如何查询域名到期时间?
  • 毛桃Pe系统教程(打造专属于你的电脑系统,让操作更高效、更顺心)
  • 门罗币挖矿僵尸网络PGMiner瞄准PostgreSQL