代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
处理ip的工具类
作者:
库特
/ 发布于
2014/9/10
/
286
package com.hh.test; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; /** * IP工具类 * * @author bl * @email kutekute00@gmail.com * */ 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; } }
试试其它关键字
处理ip
工具类
ip
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
库特
贡献的其它代码
(
1
)
.
处理ip的工具类
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3