标签:5.0 subnetList java iterator jar new address import ipaddress
package test; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import inet.ipaddr.IPAddress; import inet.ipaddr.IPAddressString; import inet.ipaddr.ipv4.IPv4Address; public class TestIpAddr { public static void main(String[] args) { //ip转bigint IPAddress address = new IPAddressString("192.168.1.1").getAddress() ; System.out.println( address.getValue() ); // bigint转ip System.out.println(String.join(".",new IPv4Address(toByteArray(3232235777L)).getSegmentStrings())); //获取子网 List<String> subnetList = getSubnetList("192.168.1.1/255.255.252.0"); for(int i=0;i<subnetList.size() ; i++ ) { System.out.println(subnetList.get(i) ); } } private static byte[] toByteArray(Long a){ ByteArrayOutputStream bos=new ByteArrayOutputStream(); DataOutputStream dos=new DataOutputStream(bos); try { dos.writeLong(a); } catch (IOException e) { e.printStackTrace(); } return bos.toByteArray(); } public static List <String> getSubnetList (String ipAddr){ IPAddress address = new IPAddressString(ipAddr).getAddress(); int prefixLength=address.toAddressString().getNetworkPrefixLength(); List<String> subnetList = new ArrayList<String>(); IPAddress subnet = address.applyPrefixLength(prefixLength).toPrefixBlock(); Iterator<? extends IPAddress> iterator = subnet.iterator(); while (iterator.hasNext()) { subnetList.add(iterator.next().toCanonicalWildcardString()); } subnetList.remove(0); subnetList.remove(subnetList.size()-1); return subnetList; } }
jar包下载地址:
https://www.mvnjar.com/com.github.seancfoley/ipaddress/5.0.2/detail.html
标签:5.0,subnetList,java,iterator,jar,new,address,import,ipaddress 来源: https://blog.51cto.com/204222/2417972
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。