在项目中使用到IP转换的方法,涉及到IPV6和IPV4,自己把写的代码记录一下,当做笔记记录下来,不一定正确,仅供参考
package com.my.utils; import java.math.BigInteger; import java.util.StringTokenizer; import java.util.regex.Pattern; public class IPUtils { /** * @Description: 将字符串类型的IP地址转换为十进制数字 * @author xj * @date 创建时间:2017-12-6 上午9:28:38 * @param SIP * @return 转换后的十进制数字字符串 * @version V1.0 */ public static String ip2Num(final String SIP) { if (SIP == null || SIP.equals("")) { return null; } // ipv6转换 int index = SIP.indexOf(":"); if (index != -1) {// 是ipv6地址 int compressIndex = SIP.indexOf("::"); if (compressIndex != -1) { String part1s = SIP.substring(0, compressIndex); String part2s = SIP.substring(compressIndex + 1); BigInteger part1 = ipv6ToNum(part1s); BigInteger part2 = ipv6ToNum(part2s); int part1hasDot = 0; char ch[] = part1s.toCharArray(); for (char c : ch) { if (c == ':') { part1hasDot++; } } // ipv6 has most 7 dot return String.valueOf(part1.shiftLeft(16 * (7 - part1hasDot)) .add(part2)); } String[] str = SIP.split(":"); BigInteger big = BigInteger.ZERO; for (int i = 0; i < str.length; i++) { // ::1 if (str[i].isEmpty()) { str[i] = "0"; } big = big.add(BigInteger.valueOf(Long.valueOf(str[i], 16)) .shiftLeft(16 * (str.length - i - 1))); } return String.valueOf(big); } // Ipv4地址 Long ip = null; int i = 3; StringTokenizer src = new StringTokenizer(SIP, "."); while (src.hasMoreTokens()) { String temp = src.nextToken(); if (i == 3) { ip = (long) (new Integer(temp).intValue() * Math.pow(256, i)); } else { ip = (long) (ip + (new Integer(temp).intValue() * Math.pow(256, i))); } i--; } return String.valueOf(ip); } /** * @Description: 比较字符串格式IP大小 * @author xj * @date 创建时间:2017-12-6 上午9:28:38 * @param ip1 * @param ip2 * @return ip1小于ip2则返回-1;ip1等于ip2则返回0;ip1大于ip2则返回1; 数据错误返回-2 * @version V1.0 */ public static int compareIP(final String ip1, final String ip2) { if (!(checkStrIsIP(ip1) && checkStrIsIP(ip2))) { return -2; } if (ip1.equals(ip2)) {// 相等 return 0; } if (ip1.contains(".") && ip2.contains(".")) {// 都表示IPV4 return ipV4ToLong(ip1).compareTo(ipV4ToLong(ip2)); } if (ip1.contains(":") && ip2.contains(":")) {// 都表示IPV6 return ipv6ToNum(ip1).compareTo(ipv6ToNum(ip2)); } return -2; } /** * @Description: 判断IP格式是否正确,IPV4和IPV6均可 * @author xj * @date 创建时间:2017-12-6 上午11:30:29 * @param str * @return * @version V1.0 */ public static boolean checkStrIsIP(String str) { Pattern patternV4 = Pattern .compile("^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]" + "|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$"); Pattern patternV6 = Pattern .compile("^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:)|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}){1,2})|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){1,3})|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){1,4})|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){1,5})|([0-9A-Fa-f]{1,4}:(:[0-9A-Fa-f]{1,4}){1,6})|(:(:[0-9A-Fa-f]{1,4}){1,7})|(([0-9A-Fa-f]{1,4}:){6}(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){5}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|([0-9A-Fa-f]{1,4}:(:[0-9A-Fa-f]{1,4}){0,4}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(:(:[0-9A-Fa-f]{1,4}){0,5}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}))$"); return patternV4.matcher(str).matches()||patternV6.matcher(str).matches(); } /** * @Description: 将IPV4转为Long型IP * @author xj * @date 创建时间:2017-12-6 上午9:57:03 * @param SIP * @return * @version V1.0 */ public static Long ipV4ToLong(final String SIP) { Long ip = null; int i = 3; StringTokenizer src = new StringTokenizer(SIP, "."); while (src.hasMoreTokens()) { String temp = src.nextToken(); if (i == 3) { ip = (long) (new Integer(temp).intValue() * Math.pow(256, i)); } else { ip = (long) (ip + (new Integer(temp).intValue() * Math.pow(256, i))); } i--; } return ip; } /** * @Description: 将IPV6转为BigInteger型IP * @author xj * @date 创建时间:2017-12-6 上午9:57:03 * @param SIP * @return * @version V1.0 */ public static BigInteger ipv6ToNum(final String ipv6) { int compressIndex = ipv6.indexOf("::"); if (compressIndex != -1) { String part1s = ipv6.substring(0, compressIndex); String part2s = ipv6.substring(compressIndex + 1); BigInteger part1 = ipv6ToNum(part1s); BigInteger part2 = ipv6ToNum(part2s); int part1hasDot = 0; char ch[] = part1s.toCharArray(); for (char c : ch) { if (c == ':') { part1hasDot++; } } return part1.shiftLeft(16 * (7 - part1hasDot)).add(part2); } String[] str = ipv6.split(":"); BigInteger big = BigInteger.ZERO; for (int i = 0; i < str.length; i++) { if (str[i].isEmpty()) { str[i] = "0"; } big = big.add(BigInteger.valueOf(Long.valueOf(str[i], 16)) .shiftLeft(16 * (str.length - i - 1))); } return big; } /** * @Description: 将BigInteger的IPV6还原 * @author xj * @date 创建时间:2017-12-6 上午9:57:03 * @param SIP * @return * @version V1.0 */ public static String int2ipv6(String num) { BigInteger big = new BigInteger(num); String str = ""; BigInteger ff = BigInteger.valueOf(0xffff); for (int i = 0; i < 8; i++) { str = big.and(ff).toString(16) + ":" + str; big = big.shiftRight(16); } str = str.substring(0, str.length() - 1); return str; } /** * 将10进制的IPV4转成标准IP格式 * * @param longIP * 10进制的IP数 * @return 返回标准IP格式 */ public static String longToIP(Long longIP) // 将10进制整数形式转换成127.0.0.1形式的IP地址 { StringBuffer sb = new StringBuffer(""); // 直接右移24位 sb.append(String.valueOf(longIP >>> 24)); sb.append("."); // 将高8位置0,然后右移16位 sb.append(String.valueOf((longIP & 0x00FFFFFF) >>> 16)); sb.append("."); sb.append(String.valueOf((longIP & 0x0000FFFF) >>> 8)); sb.append("."); sb.append(String.valueOf(longIP & 0x000000FF)); return sb.toString(); } public static void main(String[] args) { String ip1 = "1.1.1.1"; String ip2 = "1.1.1.12"; System.out.println(ip2Num(ip1)); System.out.println(ip2Num(ip2)); System.out.println(compareIP(ip1, ip2)); System.out.println(longToIP(Long.valueOf(ip2Num(ip1)))); ip1 = "1::0"; ip2 = "1::1"; System.out.println(ip2Num(ip1)); System.out.println(ip2Num(ip2)); System.out.println(compareIP(ip2, ip1)); System.out.println(int2ipv6(ip2Num(ip1))); } }