-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkUtil.java
More file actions
107 lines (86 loc) · 3.39 KB
/
Copy pathNetworkUtil.java
File metadata and controls
107 lines (86 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.logging.Logger;
/**
* @author Jason Ge Wu
* @since 2019-09-06
* @version 1.0.2
*/
public final class NetworkUtil {
private static final Logger log = Logger.getGlobal();
private static final String DEFAULT_IP = "127.0.0.1";
private static final String DEFAULT_MAC = "00-00-00-00-00-00";
public static String getLocalHostName() {
try {
InetAddress address = InetAddress.getLocalHost();
return address.getHostName();
} catch (UnknownHostException err) {
log.info(String.format("Get Localhost Name Failed: %s", err.getMessage()));
return "";
}
}
/**
* Common Method to Get Localhost IP Address
* @return Localhost IP Address
*/
public static String getLocalHostIp() {
try {
InetAddress address = InetAddress.getLocalHost();
return address.getHostAddress();
} catch (UnknownHostException err) {
log.info(String.format("Get Localhost IP Failed: %s, System Return Default [%s]",
err.getMessage(), DEFAULT_IP));
return DEFAULT_IP;
}
}
public static String getIpByHost(String hostname) throws UnknownHostException {
if (hostname == null || hostname.trim().isEmpty()) {
return getLocalHostName();
}
InetAddress address = InetAddress.getByName(hostname);
return address.getHostAddress();
}
/**
* Common Method to Get Localhost Mac Address
* @return Localhost Mac Address
*/
public static String getMacAddress() {
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(address);
byte[] macAddress = network.getHardwareAddress();
if (macAddress == null || macAddress.length == 0) {
return DEFAULT_MAC;
}
return convertMacAddressToString(macAddress);
} catch (UnknownHostException | SocketException err) {
log.info(String.format("Get Mac Address Failed: %s, System Return Default [%s]",
err.getMessage(), DEFAULT_MAC));
return DEFAULT_MAC;
}
}
public static String getMacAddress(String hostname) throws UnknownHostException, SocketException {
if (hostname == null || hostname.trim().isEmpty()) {
return getMacAddress();
}
InetAddress address = InetAddress.getByName(hostname);
if (address == null) {
throw new UnknownHostException("InetAddress is Null");
}
NetworkInterface network = NetworkInterface.getByInetAddress(address);
byte[] macAddress = network.getHardwareAddress();
if (macAddress == null || macAddress.length == 0) {
throw new IllegalStateException("Mac Address is Invalid");
}
return convertMacAddressToString(macAddress);
}
private static String convertMacAddressToString(byte[] macAddress) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < macAddress.length; i++) {
sb.append(String.format("%02X%s", macAddress[i], (i < macAddress.length - 1) ? "-" : ""));
}
return sb.toString();
}
}