Java基础之java处理ip的工具类
程序员文章站
2024-03-01 21:59:28
java处理ip的工具类,包括把long类型的ip转为一般ip类型、把xx.xx.xx.xx类型的转为long类型、根据掩码位获取掩码、根据 ip/掩码位 计算ip段的起始...
java处理ip的工具类,包括把long类型的ip转为一般ip类型、把xx.xx.xx.xx类型的转为long类型、根据掩码位获取掩码、根据 ip/掩码位 计算ip段的起始ip、根据 ip/掩码位 计算ip段的终止ip等方法,可以直接使用!
复制代码 代码如下:
package com.hh.test;
import java.util.hashmap;
import java.util.map;
import org.apache.commons.lang3.stringutils;
/**
* ip工具类
*
* @author bl
*
*/
public class iputils
{
/**
* 把long类型的ip转为一般ip类型:xx.xx.xx.xx
*
* @param ip
* @return
*/
public static string getipfromlong(long ip)
{
string s1 = string.valueof((ip & 4278190080l) / 16777216l);
string s2 = string.valueof((ip & 16711680l) / 65536l);
string s3 = string.valueof((ip & 65280l) / 256l);
string s4 = string.valueof(ip & 255l);
return s1 + "." + s2 + "." + s3 + "." + s4;
}
/**
* 把xx.xx.xx.xx类型的转为long类型的
*
* @param ip
* @return
*/
public static long getipfromstring(string ip)
{
long iplong = 0l;
string iptemp = ip;
iplong = iplong * 256
+ long.parselong(iptemp.substring(0, iptemp.indexof(".")));
iptemp = iptemp.substring(iptemp.indexof(".") + 1, iptemp.length());
iplong = iplong * 256
+ long.parselong(iptemp.substring(0, iptemp.indexof(".")));
iptemp = iptemp.substring(iptemp.indexof(".") + 1, iptemp.length());
iplong = iplong * 256
+ long.parselong(iptemp.substring(0, iptemp.indexof(".")));
iptemp = iptemp.substring(iptemp.indexof(".") + 1, iptemp.length());
iplong = iplong * 256 + long.parselong(iptemp);
return iplong;
}
/**
* 根据掩码位获取掩码
*
* @param maskbit
* 掩码位数,如"28"、"30"
* @return
*/
public static string getmaskbymaskbit(string maskbit)
{
return stringutils.isempty(maskbit) ? "error, maskbit is null !"
: maskbitmap().get(maskbit);
}
/**
* 根据 ip/掩码位 计算ip段的起始ip 如 ip串 218.240.38.69/30
*
* @param ip
* 给定的ip,如218.240.38.69
* @param maskbit
* 给定的掩码位,如30
* @return 起始ip的字符串表示
*/
public static string getbeginipstr(string ip, string maskbit)
{
return getipfromlong(getbeginiplong(ip, maskbit));
}
/**
* 根据 ip/掩码位 计算ip段的起始ip 如 ip串 218.240.38.69/30
*
* @param ip
* 给定的ip,如218.240.38.69
* @param maskbit
* 给定的掩码位,如30
* @return 起始ip的长整型表示
*/
public static long getbeginiplong(string ip, string maskbit)
{
return getipfromstring(ip) & getipfromstring(getmaskbymaskbit(maskbit));
}
/**
* 根据 ip/掩码位 计算ip段的终止ip 如 ip串 218.240.38.69/30
*
* @param ip
* 给定的ip,如218.240.38.69
* @param maskbit
* 给定的掩码位,如30
* @return 终止ip的字符串表示
*/
public static string getendipstr(string ip, string maskbit)
{
return getipfromlong(getendiplong(ip, maskbit));
}
/**
* 根据 ip/掩码位 计算ip段的终止ip 如 ip串 218.240.38.69/30
*
* @param ip
* 给定的ip,如218.240.38.69
* @param maskbit
* 给定的掩码位,如30
* @return 终止ip的长整型表示
*/
public static long getendiplong(string ip, string maskbit)
{
return getbeginiplong(ip, maskbit)
+ ~getipfromstring(getmaskbymaskbit(maskbit));
}
/**
* 根据子网掩码转换为掩码位 如 255.255.255.252转换为掩码位 为 30
*
* @param netmarks
* @return
*/
public static int getnetmask(string netmarks)
{
stringbuffer sbf;
string str;
int inetmask = 0, count = 0;
string[] iplist = netmarks.split("\\.");
for (int n = 0; n < iplist.length; n++)
{
sbf = tobin(integer.parseint(iplist[n]));
str = sbf.reverse().tostring();
count = 0;
for (int i = 0; i < str.length(); i++)
{
i = str.indexof('1', i);
if (i == -1)
{
break;
}
count++;
}
inetmask += count;
}
return inetmask;
}
/**
* 计算子网大小
*
* @param netmask
* 掩码位
* @return
*/
public static int getpoolmax(int maskbit)
{
if (maskbit <= 0 || maskbit >= 32)
{
return 0;
}
return (int) math.pow(2, 32 - maskbit) - 2;
}
private static stringbuffer tobin(int x)
{
stringbuffer result = new stringbuffer();
result.append(x % 2);
x /= 2;
while (x > 0)
{
result.append(x % 2);
x /= 2;
}
return result;
}
/*
* 存储着所有的掩码位及对应的掩码 key:掩码位 value:掩码(x.x.x.x)
*/
private static map<string, string> maskbitmap()
{
map<string, string> maskbit = new hashmap<string, string>();
maskbit.put("1", "128.0.0.0");
maskbit.put("2", "192.0.0.0");
maskbit.put("3", "224.0.0.0");
maskbit.put("4", "240.0.0.0");
maskbit.put("5", "248.0.0.0");
maskbit.put("6", "252.0.0.0");
maskbit.put("7", "254.0.0.0");
maskbit.put("8", "255.0.0.0");
maskbit.put("9", "255.128.0.0");
maskbit.put("10", "255.192.0.0");
maskbit.put("11", "255.224.0.0");
maskbit.put("12", "255.240.0.0");
maskbit.put("13", "255.248.0.0");
maskbit.put("14", "255.252.0.0");
maskbit.put("15", "255.254.0.0");
maskbit.put("16", "255.255.0.0");
maskbit.put("17", "255.255.128.0");
maskbit.put("18", "255.255.192.0");
maskbit.put("19", "255.255.224.0");
maskbit.put("20", "255.255.240.0");
maskbit.put("21", "255.255.248.0");
maskbit.put("22", "255.255.252.0");
maskbit.put("23", "255.255.254.0");
maskbit.put("24", "255.255.255.0");
maskbit.put("25", "255.255.255.128");
maskbit.put("26", "255.255.255.192");
maskbit.put("27", "255.255.255.224");
maskbit.put("28", "255.255.255.240");
maskbit.put("29", "255.255.255.248");
maskbit.put("30", "255.255.255.252");
maskbit.put("31", "255.255.255.254");
maskbit.put("32", "255.255.255.255");
return maskbit;
}
/**
* 根据掩码位获取掩码
*
* @param masks
* @return
*/
@deprecated
public static string getmaskbymaskbit(int masks)
{
string ret = "";
if (masks == 1)
ret = "128.0.0.0";
else if (masks == 2)
ret = "192.0.0.0";
else if (masks == 3)
ret = "224.0.0.0";
else if (masks == 4)
ret = "240.0.0.0";
else if (masks == 5)
ret = "248.0.0.0";
else if (masks == 6)
ret = "252.0.0.0";
else if (masks == 7)
ret = "254.0.0.0";
else if (masks == 8)
ret = "255.0.0.0";
else if (masks == 9)
ret = "255.128.0.0";
else if (masks == 10)
ret = "255.192.0.0";
else if (masks == 11)
ret = "255.224.0.0";
else if (masks == 12)
ret = "255.240.0.0";
else if (masks == 13)
ret = "255.248.0.0";
else if (masks == 14)
ret = "255.252.0.0";
else if (masks == 15)
ret = "255.254.0.0";
else if (masks == 16)
ret = "255.255.0.0";
else if (masks == 17)
ret = "255.255.128.0";
else if (masks == 18)
ret = "255.255.192.0";
else if (masks == 19)
ret = "255.255.224.0";
else if (masks == 20)
ret = "255.255.240.0";
else if (masks == 21)
ret = "255.255.248.0";
else if (masks == 22)
ret = "255.255.252.0";
else if (masks == 23)
ret = "255.255.254.0";
else if (masks == 24)
ret = "255.255.255.0";
else if (masks == 25)
ret = "255.255.255.128";
else if (masks == 26)
ret = "255.255.255.192";
else if (masks == 27)
ret = "255.255.255.224";
else if (masks == 28)
ret = "255.255.255.240";
else if (masks == 29)
ret = "255.255.255.248";
else if (masks == 30)
ret = "255.255.255.252";
else if (masks == 31)
ret = "255.255.255.254";
else if (masks == 32)
ret = "255.255.255.255";
return ret;
}
}