您好,登錄后才能下訂單哦!
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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。