Utils-IPUtil 处理ip聚、拆、起、终、区间计算等工具类

  • 处理ip聚、拆、起、终、区间计算等工具类
  • maven依赖
        <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.70</version>
		</dependency>
        <dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.8.1</version>
		</dependency>
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
  • ip处理工具类的返回封装实体类 
package com.utils;

import java.io.Serializable;

import com.alibaba.fastjson.JSONObject;
@SuppressWarnings("serial")
public class Interval implements Serializable, Comparable<Interval> {
	      long start, end;
	      String ipStart;
	      String ipEnd;
	      String ips;
	      long size;
	      
	      
	      
	      
	    public String getIpStart() {
			return ipStart;
		}
		public void setIpStart(String ipStart) {
			this.ipStart = ipStart;
		}
		public String getIpEnd() {
			return ipEnd;
		}
		public void setIpEnd(String ipEnd) {
			this.ipEnd = ipEnd;
		}
		public long getSize() {
			return size;
		}
		public void setSize(long size) {
			this.size = size;
		}
		public String getIps() {
			return ips;
		}
		public void setIps(String ips) {
			this.ips = ips;
		}
		public Interval(long start, long end) {
	          this.start = start;
	          this.end = end;
	      }
		
		public Interval(String ipStart,String ipEnd,long start, long end) {
	          this.start = start;
	          this.end = end;
	          this.ipStart = ipStart;
	          this.ipEnd = ipEnd;
	          this.size = end -start +1;
	      }
		public Interval() {

	      }
		public long getStart() {
			return start;
		}
		public void setStart(long start) {
			this.start = start;
		}
		public long getEnd() {
			return end;
		}
		public void setEnd(long end) {
			this.end = end;
		}

		@Override
		public String toString(){
			JSONObject obj = new JSONObject();
			obj.put("start", this.getStart());
			obj.put("end", this.getEnd());
			obj.put("ipStart", this.getIpStart());
			obj.put("ipEnd", this.getIpEnd());
			return obj.toString();
			
		}
		/*@Override
		public int compareTo(Interval o) {
			Long it = this.start - o.start;
			int i = it.intValue();
			return i;
		} bak*/

		@Override
		public int compareTo(Interval o) {

			if (this.start > o.start)
				return 1;
			if (this.start < o.start)
				return -1;
			if (this.end > o.end)
				return 1;
			if (this.end < o.end)
				return -1;
			return 0;
		}
	
}
  •  ip处理工具类
package com.utils;


import com.exception.MyException;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
* IP工具类
* 
* @author bl
* @email kutekute00@gmail.com
* 
*/
public class IpUtil {
	//192.168.1.1/24
	private static final String maskStr = "(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\\/(\\d|([1,2]\\d)|(3[0-2]))";
	//102.168.1.1-255
	private static final String rangStr = "(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))-((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))";
	//192.168.1.1
	private static final String ipStr = "(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))";
	//102.168.1.1-102.168.8.254
	private static final String startAndEndStr = "(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))-(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))";
	
	private static final String[] STANDARDIPS={"1/24","1-","-254"};
	
	private static final String[] SPECIALIPS={"0/24","0-","-255"};
	/**
	 * 判断ip段是否在指定ip段范围内
	 * @param parentIps
	 * @param childIps
	 * @return
	 * @throws Exception
	 */
	public static boolean ipContains (String parentIps,String childIps)throws Exception{
		List<Interval> parentList = getIpIntervalList(parentIps);
		List<Interval> childList = getIpIntervalList(childIps);
		int successNum = 0;
		if(childList!=null && childList.size()>0){
			for(Interval child:childList){
				if(IntervalUtil.contains(parentList, child)){
					successNum++;
				}
			}
		}
		if(successNum==childList.size()){
			return true;
		}else{
			return false;
		}
	}
	
	

	/**
	 * 判断ip段是否在指定ip段范围内
	 * @param intervalList
	 * @param ips
	 * @return
	 * @throws Exception
	 */
	public static boolean ipContains (List<Interval> intervalList, String ips)throws Exception{
		List<Interval> childList = getIpIntervalList(ips);
		int successNum = 0;
		if(childList!=null && childList.size()>0){
			for(Interval child:childList){
				if(IntervalUtil.contains(intervalList, child)){
					successNum++;
				}else{
					throw new RuntimeException(child.getIps()+"不在授权扫描的范围内");
				}
			}
		}
		if(successNum==childList.size()){
			return true;
		}else{
			return false;
		}
	}


	/**
	 * 在指定ip段范围内去除指定IP
	 * @param parentIps
	 * @param childIps
	 * @return
	 * @throws Exception
	 */
	public static String ipWipeOut (String parentIps,String childIps)throws Exception{
		List<Map<String, Object>> parentIpsList = getStartAndEndIpLong(parentIps);

		List<Map<String, Object>> childIpsList = getStartAndEndIpLong(childIps);
		List<String> newparentList=new ArrayList<>();
		List<String> newchildList=new ArrayList<>();

		for (Map<String, Object> stringObjectMap : parentIpsList) {
			List<String> iplist = (List<String>) stringObjectMap.get("IPLIST");
			if (!iplist.isEmpty()){
				newparentList=Stream.of(iplist,newparentList).flatMap(Collection::stream).distinct().collect(Collectors.toList());
			}
		}

		for (Map<String, Object> stringObjectMap : childIpsList) {
			List<String> iplist = (List<String>) stringObjectMap.get("IPLIST");
			if (!iplist.isEmpty()){
				newchildList=Stream.of(iplist,newchildList).flatMap(Collection::stream).distinct().collect(Collectors.toList());
			}
		}

		for (String s : newchildList) {
			for (int i = 0; i < newparentList.size(); i++) {
				if(s.equals(newparentList.get(i))){
					newparentList.remove(i);
					i--;
				}
			}
		}

		String parentIpsStr = StringUtils.join(newparentList.toArray(), ";");
		List<Interval> resultParent = getIpIntervalList(parentIpsStr);
		List<Interval> merge = IntervalUtil.merge(resultParent);

		List<String> tmp2=new ArrayList<>();
		for (Interval interval : merge) {
			long start = interval.getStart();
			long end = interval.getEnd();
			tmp2.add(start+"-"+end);
		}
		String[] objects = (String[])tmp2.toArray(new String[tmp2.size()]);
		String[] strings = IpsMerge.devideBIp(objects);
		String join = StringUtils.join(strings, ";");

		return join;

	}

	/**
	 * 判断ip段是否在指定ip段范围内(并不在范围内的列出)
	 * @param intervalList
	 * @param ips
	 * @return
	 * @throws Exception
	 */
	public static Set<String> ipContains2(List<Interval> intervalList, String ips) throws Exception {

		List<Interval> childList = getIpIntervalList(ips);
		int successNum = 0;
		Set<String> outIps=new HashSet<>();
		if(childList!=null && childList.size()>0){
			for(Interval child:childList){
				if(IntervalUtil.contains(intervalList, child)){
					successNum++;
				}else{
					outIps.add(child.getIps());
				}
			}
		}
		if (outIps.size()>0){
			return outIps;
		}

		return null;
	}
	public static String ipWipeOut2 (String parentIps,String childIps)throws Exception{
		List<Map<String, Object>> parentIpsList = getStartAndEndIpLong(parentIps);

		List<Map<String, Object>> childIpsList = getStartAndEndIpLong(childIps);
		List<String> newparentList=new ArrayList<>();
		List<String> newchildList=new ArrayList<>();

		for (Map<String, Object> stringObjectMap : parentIpsList) {
			List<String> iplist = (List<String>) stringObjectMap.get("IPLIST");
			if (!iplist.isEmpty()){
				newparentList=Stream.of(iplist,newparentList).flatMap(Collection::stream).distinct().collect(Collectors.toList());
			}
		}

		for (Map<String, Object> stringObjectMap : childIpsList) {
			List<String> iplist = (List<String>) stringObjectMap.get("IPLIST");
			if (!iplist.isEmpty()){
				newchildList=Stream.of(iplist,newchildList).flatMap(Collection::stream).distinct().collect(Collectors.toList());
			}
		}

		for (String s : newchildList) {
			for (int i = 0; i < newparentList.size(); i++) {
				if(s.equals(newparentList.get(i))){
					newparentList.remove(i);
					i--;
				}
			}
		}

		String parentIpsStr = StringUtils.join(newparentList.toArray(), ";");
		List<Interval> resultParent = getIpIntervalList(parentIpsStr);
		List<Interval> merge = IntervalUtil.merge(resultParent);

		List<String> tmp2=new ArrayList<>();
		for (Interval interval : merge) {
			long start = interval.getStart();
			String ipFromLong = getIpFromLong(start);
			long end = interval.getEnd();
			String ipFromLong1 = getIpFromLong(end);
			tmp2.add(ipFromLong+"-"+ipFromLong1);
		}
		String[] objects = (String[])tmp2.toArray(new String[tmp2.size()]);
		//String[] strings = IpsMerge.devideBIp(objects);
		String join = StringUtils.join(objects, ";");

		return join;

	}

//	public static void main(String[] args) throws Exception {
//		//String s = ipWipeOut2("10.1.0.0/16", "10.1.20.0/24;10.1.30.0/24;10.1.40.0/24;10.1.50.0/24;");
//		//String s1 = ipWipeOut2("10.2.0.0/16", "10.2.2.0/24;10.2.4.0/24;10.2.28.0/24;10.2.40.0/24;10.2.52.0/24;10.2.102.0/24;10.2.106.0/24;10.2.108.0/24;10.2.110.0/24;10.2.112.0/24;10.2.114.0/24;10.2.118.0/24;10.2.104.0/24;10.2.116.0/24");
//		String s2 = ipWipeOut2("10.3.0.0/16", "10.3.22.0/23;10.3.26.0/24;10.3.36.0/24;10.3.128.0/24;10.3.130.0/24");
//
//		//String ipFromLong = getIpFromLong(Long.valueOf("167837696"));
//		System.out.println(s2);
//	}

//	public static void main(String[] args) throws Exception {
//		String s = ipWipeOut("11.0.34.211-11.0.34.213;11.0.34.215-11.0.34.227", "11.0.34.211;11.0.34.222-223");
//		System.out.println(s);
//	}
    
//    public static void main(String[] args) {
        String ip="192.168.1.1";
        String mask="24";
        System.out.println(parseIpMaskRange(ip, mask).size());;
//    	try {
//    		// 本页调试spring未启动,不能调试查询语句
    		List<Map<String,Object>> ipList = new UtilService().getSysRoleIpDetail("029db5a47d7f4d2c960932bb0ac648f9");
    		String ipList = new UtilService().getRoleTaskAble("4c329362bd074af7aeaa6c1a17161744","");
			getStartAndEndIpLong("192.168.1.1/24");
			192.168.0.0-192.168.6.254;
			192.168.0-6.*;
			192.0-6.*.*;
			192.*;
			192.168.8;
			System.out.println(ipContains("1.11.1.1-11.11.254.145;1.11.1.1-11.11.254.145;","11.11.184.144"));
//			List<Interval> list = getIpIntervalList("11.11.184.144;11.11.184.145;11.11.183.140-144;11.11.183.145;");
//			System.out.println(IntervalUtil.merge(list));
    		System.out.println(InetAddress.getByName("10.86.14.7/jdtimas/" ));
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//    }
    

    /**
     * 获取ipv各段中Ip列表
     * @param ipv
     * @return
     */
    @SuppressWarnings("unchecked")
	public static List<String> getIpListAll(String ipv){
    	List<String> ipListAll = new ArrayList<String>();
    	try{
    		
    	List<Map<String,Object>> listIpMap = getStartAndEndIpLong(ipv);
			for(Map<String,Object> ipMap : listIpMap){
				ipListAll.addAll((List<String>)ipMap.get("IPLIST"));
			}
    	}catch (Exception e) {e.printStackTrace();
    		ipListAll = null;
    	}
    	return ipListAll;
    }
    
    /**
     * 校验ip地址,注意不是地址段
     * @param ip
     * @return
     */
    public static boolean virifyIp(String ip){
    	ip = ip.replaceAll("[\\r\\n\\s]", "");
		String maskStr = "(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))";
		Pattern pattern = Pattern.compile(maskStr);
		Matcher matcher = pattern.matcher(ip);
		return matcher.matches();
		
		}
    /**
     * 获取ip段中的所有ip地址
     * @param ipv
     * @return
     * @throws Exception
     */
    public static List<Interval> getIpIntervalList(String ipv)throws Exception{
    	ipv = ipv.replaceAll("[\\r\\n\\s]", "");
    	List<Interval> list = new ArrayList<Interval>();
    	if(StringUtils.isNotEmpty(ipv)){
    		String ips[] = ipv.split(";");
    		if(ips.length>0){
    			for(String ip : ips){
    				//去掉空格,回车
    
    				if(StringUtils.isNotEmpty(ip)){
    					List<String> listStr = new ArrayList<String>();
        				listStr.add(maskStr);
        				listStr.add(rangStr);
        				listStr.add(ipStr);
        				listStr.add(startAndEndStr);
        				boolean flag = false;
        				for(int i =0 ;i<listStr.size();i++){
        					String ipRange = listStr.get(i);
        						Pattern pattern = Pattern.compile(ipRange);
                				Matcher matcher = pattern.matcher(ip);
                				if(matcher.matches()){
                					if(i==0){
                						Interval interval = getIpMaskInterval(ip);
                        				list.add(interval);
                        				flag = true;
                					}else if(i==1){
                						Interval interval = getIpRangeInterval(ip);
                        				list.add(interval);
                        				flag = true;
                					}else if(i==2){
                						Interval interval = getIpInterval(ip);
                        				list.add(interval);
                        				flag = true;
                					}else if(i==3){
                						Interval interval = getStartAndEndInterval(ip);
                						list.add(interval);
                        				flag = true;
                					}
                				}
        				}
        				if(!flag){
        					throw new RuntimeException("ip段:"+ip+"不符合格式要求");
        				}
    				}
    			}
    		}
    	}
    	return list;
    }

//	public static void main(String[] args) throws Exception {
//		List<Map<String, Object>> startAndEndIpLong = getStartAndEndIpLong("11.0.34.196-220;11.11.54.12/24;11.11.164.114;11.11.184.143-11.11.184.145;");
//		List<String> newList=new ArrayList<>();
//
//		for (Map<String, Object> stringObjectMap : startAndEndIpLong) {
//			List<String> iplist = (List<String>) stringObjectMap.get("IPLIST");
//			if (!iplist.isEmpty()){
//				newList=Stream.of(iplist,newList).flatMap(Collection::stream).distinct().collect(Collectors.toList());
//			}
//		}
//		System.out.println(newList.toString());
//	}
//
    /**
     * 获取ip段中的所有ip地址
     * @param ipv
     * @return
     * @throws Exception
     */
    public static List<Map<String,Object>> getStartAndEndIpLong(String ipv)throws Exception{
    	//ipv=changeIpToStandard(ipv);
    	ipv = ipv.replaceAll("[\\r\\n\\s]", "");
    	List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
    	Set<String> ipSet = new HashSet<String>();
    	if(StringUtils.isNotEmpty(ipv)){
    		String ips[] = ipv.split(";");
    		if(ips.length>0){
    			for(String ip : ips){
    				//去掉空格,回车
//    				ip = ip.replaceAll("\r", "").replaceAll("\n", "").replaceAll(" ", "");
    				if(ipSet.contains(ip)){
    					throw new MyException("REPEAT IP","ip段:"+ip+"重复");
    				}else{
    					ipSet.add(ip);
    				}
    				if(StringUtils.isNotEmpty(ip)){
    					List<String> listStr = new ArrayList<String>();
        				listStr.add(maskStr);
        				listStr.add(rangStr);
        				listStr.add(ipStr);
        				listStr.add(startAndEndStr);
        				boolean flag = false;
        				for(int i =0 ;i<listStr.size();i++){
        					String ipRange = listStr.get(i);
        					Pattern pattern = Pattern.compile(ipRange);
            				Matcher matcher = pattern.matcher(ip);
            				if(matcher.matches()){
            					if(i==0){
            						Map<String,Object> map = getSADIpMaskRange(ip);
                    				list.add(map);
                    				flag = true;
            					}else if(i==1){
            						Map<String,Object> map = getSADIpRange(ip);
                    				list.add(map);
                    				flag = true;
            					}else if(i==2){
            						Map<String,Object> map = getSADIp(ip);
                    				list.add(map);
                    				flag = true;
            					}else if(i==3){
            						Map<String,Object> map = getSADStartAndEnd(ip);
            						list.add(map);
                    				flag = true;
            					}
            					
            				}
        				}
        				if(!flag){
        					throw new RuntimeException("ip段:"+ip+"不符合格式要求");
        				}
    				}
    				
    			}
    		}
    	}
    	return list;
    }
    /**
     * 获取子网掩码方式的ip列表
     * @param ipv
     * @return
     */
    public static Map<String,Object> getSADIpMaskRange(String ipv){
    	String ip = ipv.substring(0,ipv.indexOf("/"));
    	String mask = ipv.substring(ipv.indexOf("/")+1,ipv.length());
    	Long start =  getBeginIpLong(ip,  mask);
    	Long end =  getIpFromString(getEndIpStr(ip,  mask));
    	Map<String,Object> map = new HashMap<String,Object>();
    	List<String> ipList =  parseIpMaskRange(ip,mask);
    	map.put("START", start);
    	map.put("END", end);
    	map.put("SIZE", ipList.size());
    	map.put("IPV", ipv);
    	map.put("IPLIST", ipList);
    	return map;
    }
    
    /**
     * 获取子网掩码方式的ip区间
     * @param ipv
     * @return
     */
    public static Interval getIpMaskInterval(String ipv){
    	String ip = ipv.substring(0,ipv.indexOf("/"));
    	String mask = ipv.substring(ipv.indexOf("/")+1,ipv.length());
    	Long start =  getBeginIpLong(ip,  mask);
    	Long end =  getIpFromString(getEndIpStr(ip,  mask));
    	Interval interval = new Interval(start,end);
    	interval.setIps(ipv);
    	return interval;
    }
    
    /**
     * 获取ip段方式的ip列表
     * @param ipv
     * @return
     */
    public static Map<String,Object> getSADIpRange(String ipv){
    	//结束位
    	String endStr = ipv.substring(ipv.lastIndexOf("-")+1,ipv.length());
    	//开始位
    	String startStr = ipv.substring(ipv.lastIndexOf(".")+1,ipv.lastIndexOf("-"));
    	
    	int endInt = Integer.valueOf(endStr);
    	int startInt = Integer.valueOf(startStr);
    	
    	String ipHead = ipv.substring(0,ipv.lastIndexOf("."));
    	List<String> ipList = new ArrayList<String>();
    	for(int i = startInt;i<=endInt;i++){
    		String ip = ipHead+"."+String.valueOf(i);
    		ipList.add(ip);
    	}
    	
    	Long start = getIpFromString(ipHead+"."+startStr);
    	Long end = getIpFromString(ipHead+"."+endStr);
    	
 
    	Map<String,Object> map = new HashMap<String,Object>();
    	if (start>end){
    		Long tmp=start;
    		start=end;
    		end=tmp;
		}
    	map.put("START", start);
    	map.put("END", end);
    	map.put("IPV", ipv);
    	map.put("SIZE", ipList.size());
    	map.put("IPLIST", ipList);
    	return map;
    }
    
    
    /**
     * 获取ip段方式的ipInterval
     * @param ipv
     * @return
     */
    public static Interval getIpRangeInterval(String ipv){
    	//结束位
    	String endStr = ipv.substring(ipv.lastIndexOf("-")+1,ipv.length());
    	//开始位
    	String startStr = ipv.substring(ipv.lastIndexOf(".")+1,ipv.lastIndexOf("-"));
    	
    	int endInt = Integer.valueOf(endStr);
    	int startInt = Integer.valueOf(startStr);
    	
    	String ipHead = ipv.substring(0,ipv.lastIndexOf("."));
    	List<String> ipList = new ArrayList<String>();
    	for(int i = startInt;i<=endInt;i++){
    		String ip = ipHead+"."+String.valueOf(i);
    		ipList.add(ip);
    	}
    	
    	Long start = getIpFromString(ipHead+"."+startStr);
    	Long end = getIpFromString(ipHead+"."+endStr);
    	Interval interval = new Interval(start,end);
    	interval.setIps(ipv);
    	return interval;
    }
    
    /**
     * 根据起始,截止ip地址,获取ip列表
     * @param ipv
     * @return
     */
    public static Map<String,Object> getSADStartAndEnd(String ipv)throws Exception{
    	//结束位
    	String endStr = ipv.substring(ipv.lastIndexOf("-")+1,ipv.length());
    	//开始位
    	String startStr = ipv.substring(0,ipv.lastIndexOf("-"));
//    	
    	
    	Long start = getIpFromString(startStr);
    	Long end = getIpFromString(endStr);
    	
    	List<String> ipList = new ArrayList<String>();
    	for(Long i = start;i<=end;i++){
    		String ip = getIpFromLong(i);
    		ipList.add(ip);
    	}


		if (start>end){
			Long tmp=start;
			start=end;
			end=tmp;
		}
    	
 
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("START", start);
    	map.put("END", end);
    	map.put("IPV", ipv);
    	map.put("SIZE", ipList.size());
    	map.put("IPLIST", ipList);
    	return map;
    }
    
    
    /**
     * 根据起始,截止ip地址,获取ipInterval
     * @param ipv
     * @return
     */
    public static Interval getStartAndEndInterval(String ipv)throws Exception{
    	//结束位
    	String endStr = ipv.substring(ipv.lastIndexOf("-")+1,ipv.length());
    	//开始位
    	String startStr = ipv.substring(0,ipv.lastIndexOf("-"));
//    	
    	
    	Long start = getIpFromString(startStr);
    	Long end = getIpFromString(endStr);
    	
    	Interval interval = new Interval(start,end);
    	interval.setIps(ipv);
    	return interval;
    }
    
    /**
     * 获取单ip列表
     * @param ipv
     * @return
     */
    public static Map<String,Object> getSADIp(String ipv){
    	
    	List<String> ipList = new ArrayList<String>();

    	Long start = getIpFromString(ipv);
    	Long end = getIpFromString(ipv);
    	ipList.add(ipv);
 
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("START", start);
    	map.put("END", end);
    	map.put("SIZE", ipList.size());
    	map.put("IPV", ipv);
    	map.put("IPLIST", ipList);
    	return map;
    }
    
    /**
     * 获取单ip Interval
     * @param ipv
     * @return
     */
    public static Interval getIpInterval(String ipv){
    	Long start = getIpFromString(ipv);
    	Long end = getIpFromString(ipv);
    	Interval interval = new Interval(start,end);
    	interval.setIps(ipv);
    	return interval;
    	
    }
    
    public static String getIpRange(List<Map<String,Object>> ipList,String ip){
    	Long ipLong = getIpFromString(ip);
    	String ipRange = "";
    	if(ipList!=null && ipList.size()>0){
    		for(Map<String,Object> ips :ipList){
    			Long start = Long.valueOf(ips.get("START").toString());
    			Long end = Long.valueOf(ips.get("END").toString());
    			String ipv = ips.get("IPV").toString();
    			//ip在一个段内
    			if(ipLong>=start && ipLong<= end){
    				ipRange = ipv;
    				
    				break;
    			}
    		}
    	}
    	return ipRange;
    }
    
    public static List<String> parseIpMaskRange(String ip,String mask){
        List<String> list=new ArrayList<String>();
        if ("32".equals(mask)) {
            list.add(ip);
        }else{
            String startIp=getBeginIpStr(ip, mask);
            String endIp=getEndIpStr(ip, mask);
            
            if (!"31".equals(mask)) {
                String subStart=startIp.split("\\.")[0]+"."+startIp.split("\\.")[1]+"."+startIp.split("\\.")[2]+".";
                String subEnd=endIp.split("\\.")[0]+"."+endIp.split("\\.")[1]+"."+endIp.split("\\.")[2]+".";
                //修改
                startIp=subStart+(Integer.valueOf(startIp.split("\\.")[3]));
                //修改
                endIp=subEnd+(Integer.valueOf(endIp.split("\\.")[3]));
            }
            list=parseIpRange(startIp, endIp);
        }
        return list;
    }

    public static List<String> parseIpRange(String ipfrom, String ipto) {
        List<String> ips = new ArrayList<String>();
        String[] ipfromd = ipfrom.split("\\.");
        String[] iptod = ipto.split("\\.");
        int[] int_ipf = new int[4];
        int[] int_ipt = new int[4];
        for (int i = 0; i < 4; i++) {
            int_ipf[i] = Integer.parseInt(ipfromd[i]);
            int_ipt[i] = Integer.parseInt(iptod[i]);
        }
        for (int A = int_ipf[0]; A <= int_ipt[0]; A++) {
            for (int B = (A == int_ipf[0] ? int_ipf[1] : 0); B <= (A == int_ipt[0] ? int_ipt[1]
                    : 255); B++) {
                for (int C = (B == int_ipf[1] ? int_ipf[2] : 0); C <= (B == int_ipt[1] ? int_ipt[2]
                        : 255); C++) {
                    for (int D = (C == int_ipf[2] ? int_ipf[3] : 0); D <= (C == int_ipt[2] ? int_ipt[3]
                            : 255); D++) {
                        ips.add(new String(A + "." + B + "." + C + "." + D));
                    }
                }
            }
        }
        return ips;
    }
    
    /**
     * 把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)
    {
    	long end = getBeginIpLong(ip, maskBit)
                + ~getIpFromString(getMaskByMaskBit(maskBit));
        return end;
    }
    
    
      /**
     * 根据子网掩码转换为掩码位 如 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;
    }
    
    /**
     * 计算子网大小
     *
     *            掩码位
     * @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
     */
    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;
    }

    public static String changeIpToStandard(String ips){
    	String ip=ips;
    	for (int i = 0; i < SPECIALIPS.length; i++) {
    		ip=ip.replace(SPECIALIPS[i], STANDARDIPS[i]);
		}
    	return ip;
    }

	/**
	 * wzc add
	 * 将ip掩码段转换IP段
	 * 11.0.0.0/16 转为 11.0.0.0-11.0.255.255
	 */
	public static String convertIp(String ip){
		Interval ipMaskInterval = IpUtils.getIpMaskInterval(ip);
		String ipFromLong = IpUtils.getIpFromLong(ipMaskInterval.getStart());
		String ipFromLong1 = IpUtils.getIpFromLong(ipMaskInterval.getEnd());
		String joinIp = ipFromLong+"-"+ ipFromLong1;
		return  joinIp;
	}

	//获取用户ip
	public String getClientIp(HttpServletRequest request) {
		//X-Forwarded-For,不区分大小写
		String possibleIpStr = request.getHeader("X-Forwarded-For");
		String remoteIp = request.getRemoteAddr();
		String clientIp;
		if (StringUtils.isNotBlank(possibleIpStr) && !"unknown".equalsIgnoreCase(possibleIpStr)) {
			//可能经过好几个转发流程,第一个是用户的真实ip,后面的是转发服务器的ip
			clientIp = possibleIpStr.split(",")[0].trim();
		} else {
			//如果转发头ip为空,说明是直接访问的,没有经过转发
			clientIp = remoteIp;
		}
		return clientIp;
	}
}
  •  自定义异常类,可以用RuntimeException代替
package com.exception;

import java.text.MessageFormat;
import java.util.MissingResourceException;

/**
 * 自定义异常类 by ChenYb date 2019/4/29
 */
public class MyException extends RuntimeException {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -718639292962381679L;

    /**
     * 错误代码,默认为未知错误
     */
    protected String errorCode = "";

    /**
     * 错误信息中的参数
     */
    protected String[] errorArgs = null;

    /**
     * 兼容纯错误信息,不含error code,errorArgs的情况
     */
    protected String errorMessage = null;

    /**
     * 错误信息的i18n ResourceBundle.
     */
    public MyException() {
        super();
    }

    public MyException(String errorMessage) {
        super(errorMessage);
        this.errorMessage = errorMessage;
        this.errorCode = errorMessage;
    }

    public MyException(String message, Throwable cause) {
        super(message, cause);
    }

    public MyException(String errorCode, String errorMessage) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public MyException(ExceptionEnum ecm) {
        this.errorCode = ecm.value();
    }

    /**
     * 获得出错信息. 读取i18N properties文件中Error Code对应的message,并组合参数获得i18n的出错信息.
     */
    @Override
    public String getMessage() {
        // 如果errorMessage不为空,直接返回出错信息.
        if (errorMessage != null) {
            return errorMessage;
        }
        // 否则用errorCode查询Properties文件获得出错信息
        String message = null;
        try {
            // message = rb.getString(String.valueOf(errorCode));
        } catch (MissingResourceException mse) {
            message = "ErrorCode is: " + errorCode + ", but can't get the message of the Error Code";
        }
        // 将出错信息中的参数代入到出错信息中
        if (errorArgs != null) {
            message = MessageFormat.format(message, (Object[]) errorArgs);
        }
        return message;
    }

    /**
     * @return the errorCode
     */
    public String getErrorCode() {
        return errorCode;
    }

    /**
     * @param errorCode the errorCode to set
     */
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值