| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,14 @@ | |||
| 1 | 1 | package com.xdc.basic.commons.network; | |
| 2 | 2 | ||
| 3 | + import java.net.Inet4Address; | ||
| 4 | + import java.net.InetAddress; | ||
| 5 | + import java.net.NetworkInterface; | ||
| 6 | + import java.net.SocketException; | ||
| 7 | + import java.util.ArrayList; | ||
| 8 | + import java.util.Enumeration; | ||
| 9 | + import java.util.List; | ||
| 10 | + import java.util.TreeSet; | ||
| 11 | + | ||
| 3 | 12 | import org.apache.commons.lang3.StringUtils; | |
| 4 | 13 | import org.apache.commons.lang3.math.NumberUtils; | |
| 5 | 14 | ||
@@ -140,4 +149,40 @@ private static int ipToInt(String ipString) | |||
| 140 | 149 | ||
| 141 | 150 | return ipInt; | |
| 142 | 151 | } | |
| 152 | + | ||
| 153 | + public static List<String> getIps() throws SocketException | ||
| 154 | + { | ||
| 155 | + // 使用TreeSet:保证唯一,保证升序。 | ||
| 156 | + TreeSet<String> ipTreeSet = new TreeSet<String>(); | ||
| 157 | + | ||
| 158 | + Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); | ||
| 159 | + while (networkInterfaces.hasMoreElements()) | ||
| 160 | + { | ||
| 161 | + NetworkInterface networkInterface = networkInterfaces.nextElement(); | ||
| 162 | + if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) | ||
| 163 | + { | ||
| 164 | + continue; | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); | ||
| 168 | + while (inetAddresses.hasMoreElements()) | ||
| 169 | + { | ||
| 170 | + InetAddress inetAddress = inetAddresses.nextElement(); | ||
| 171 | + if (!(inetAddress instanceof Inet4Address)) | ||
| 172 | + { | ||
| 173 | + continue; | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + ipTreeSet.add(inetAddress.getHostAddress()); | ||
| 177 | + } | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + List<String> ipList = new ArrayList<String>(); | ||
| 181 | + for (String ip : ipTreeSet) | ||
| 182 | + { | ||
| 183 | + ipList.add(ip); | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + return ipList; | ||
| 187 | + } | ||
| 143 | 188 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,8 +5,11 @@ | |||
| 5 | 5 | import java.util.ArrayList; | |
| 6 | 6 | import java.util.Enumeration; | |
| 7 | 7 | import java.util.List; | |
| 8 | + import java.util.TreeSet; | ||
| 8 | 9 | ||
| 9 | - import org.apache.commons.lang3.StringUtils; | ||
| 10 | + import org.apache.commons.lang3.ArrayUtils; | ||
| 11 | + | ||
| 12 | + import com.xdc.basic.commons.StringUtil; | ||
| 10 | 13 | ||
| 11 | 14 | public class MacUtil | |
| 12 | 15 | { | |
@@ -32,40 +35,69 @@ public static boolean isMac(String mac) | |||
| 32 | 35 | ||
| 33 | 36 | public static boolean isMac(String mac, boolean filterSpecialMac) | |
| 34 | 37 | { | |
| 35 | - if (StringUtils.isBlank(mac)) | ||
| 38 | + if (!StringUtil.matches(mac, macRegExp)) | ||
| 36 | 39 | { | |
| 37 | 40 | return false; | |
| 38 | 41 | } | |
| 39 | 42 | ||
| 40 | - if (filterSpecialMac) | ||
| 43 | + if (filterSpecialMac && specialMacs.contains(mac)) | ||
| 41 | 44 | { | |
| 42 | - if (specialMacs.contains(mac)) | ||
| 43 | - { | ||
| 44 | - return false; | ||
| 45 | - } | ||
| 45 | + return false; | ||
| 46 | 46 | } | |
| 47 | 47 | ||
| 48 | - return mac.matches(macRegExp); | ||
| 48 | + return true; | ||
| 49 | 49 | } | |
| 50 | 50 | ||
| 51 | 51 | public static List<String> getMacs() throws SocketException | |
| 52 | 52 | { | |
| 53 | 53 | return getMacs(true); | |
| 54 | 54 | } | |
| 55 | 55 | ||
| 56 | + /** | ||
| 57 | + * 参考:http://www.jb51.net/article/93343.htm | ||
| 58 | + * 参考:http://www.cnblogs.com/KnowledgeShare/p/6184383.html | ||
| 59 | + */ | ||
| 56 | 60 | public static List<String> getMacs(boolean filterSpecialMac) throws SocketException | |
| 57 | 61 | { | |
| 58 | - // TODO 待实现 | ||
| 59 | - | ||
| 60 | - List<String> macs = new ArrayList<String>(); | ||
| 62 | + // 使用TreeSet:保证唯一,保证升序。 | ||
| 63 | + TreeSet<String> macTreeSet = new TreeSet<String>(); | ||
| 61 | 64 | ||
| 62 | 65 | Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); | |
| 63 | 66 | while (networkInterfaces.hasMoreElements()) | |
| 64 | 67 | { | |
| 65 | 68 | NetworkInterface networkInterface = networkInterfaces.nextElement(); | |
| 66 | - byte[] hardwareAddress = networkInterface.getHardwareAddress(); | ||
| 69 | + if (networkInterface.isLoopback() || networkInterface.isVirtual()) | ||
| 70 | + { | ||
| 71 | + continue; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + byte[] macBytes = networkInterface.getHardwareAddress(); | ||
| 75 | + if (ArrayUtils.isEmpty(macBytes)) | ||
| 76 | + { | ||
| 77 | + continue; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + StringBuilder sb = new StringBuilder(); | ||
| 81 | + for (int i = 0; i < macBytes.length; i++) | ||
| 82 | + { | ||
| 83 | + sb.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? "-" : "")); | ||
| 84 | + } | ||
| 85 | + String mac = sb.toString(); | ||
| 86 | + | ||
| 87 | + if (filterSpecialMac && specialMacs.contains(mac)) | ||
| 88 | + { | ||
| 89 | + continue; | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + macTreeSet.add(mac); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + List<String> macList = new ArrayList<String>(); | ||
| 96 | + for (String mac : macTreeSet) | ||
| 97 | + { | ||
| 98 | + macList.add(mac); | ||
| 67 | 99 | } | |
| 68 | 100 | ||
| 69 | - return macs; | ||
| 101 | + return macList; | ||
| 70 | 102 | } | |
| 71 | 103 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | package com.xdc.basic.commons.network; | |
| 2 | 2 | ||
| 3 | 3 | import org.junit.Assert; | |
| 4 | + import org.junit.Ignore; | ||
| 4 | 5 | import org.junit.Test; | |
| 5 | 6 | ||
| 6 | 7 | public class IpUtilTest | |
@@ -33,6 +34,7 @@ public void testIsIpv4Mask() | |||
| 33 | 34 | Assert.assertEquals(false, IpUtil.isIpv4Ip("0.0.0.0hehe")); | |
| 34 | 35 | } | |
| 35 | 36 | ||
| 37 | + @Ignore("比较耗时,不执行了。") | ||
| 36 | 38 | @Test | |
| 37 | 39 | public void testIsIpv4PortAndIsIpv4PortRegex() | |
| 38 | 40 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments