包测试; 进口java.io.ByteArrayOutputStream; 进口java.io.DataOutputStream; 进口java.io.IOException; 进口java.util.ArrayList; 进口java.util.Iterator; 进口并不知道; 进口inet.ipaddr.IPAddress; 进口inet.ipaddr.IPAddressString; 进口inet.ipaddr.ipv4.IPv4Address; 公开课TestIpAddr { 公共静态void main (String [] args) {//ip转bigint IPAddress地址=new IPAddressString (192.168.1.1) .getAddress (); system . out。println (address.getValue ());//长整型数字转ip System.out.println (String.join”。”,新IPv4Address (toByteArray (3232235777 l)) .getSegmentStrings ()));//获取子网 ListsubnetList=getSubnetList (“192.168.1.1/255.255.252.0”); for (int i=0; i< subnetList.size ();我+ +){ System.out.println (subnetList.get (i)); } } 私有静态byte [] toByteArray(长期){ ByteArrayOutputStream bos=new ByteArrayOutputStream (); DataOutputStream dos=new DataOutputStream (bos); 尝试{ dos.writeLong(一个); }捕捉(IOException e) { e.printStackTrace (); } 返回bos.toByteArray (); } 公共静态列表& lt; String>getSubnetList(字符串ipAddr) { IPAddress地址=new IPAddressString (ipAddr) .getAddress (); .getNetworkPrefixLength int prefixLength=address.toAddressString () (); List subnetList=new ArrayList (); IPAddress子网=address.applyPrefixLength (prefixLength) .toPrefixBlock (); Iterator<?IPAddress>延伸;迭代器=subnet.iterator (); 而(iterator.hasNext ()) { .toCanonicalWildcardString subnetList.add (iterator.next () ()); } subnetList.remove (0); subnetList.remove (subnetList.size () 1); 返回subnetList; } }
jar包下载地址:https://www.mvnjar.com/com.github.seancfoley/ipaddress/5.0.2/detail.html