63 lines
2.1 KiB
Java
63 lines
2.1 KiB
Java
/*
|
|||
|
|
* This file is part of the Wildfire Chat package.
|
||
|
|
* (c) Heavyrain2012 <[email protected]>
|
||
|
|
*
|
||
|
|
* For the full copyright and license information, please view the LICENSE
|
||
|
|
* file that was distributed with this source code.
|
||
|
|
*/
|
||
|
|
|
||
|
|
package win.liyufan.im;
|
||
|
|
|
||
|
|
import org.slf4j.Logger;
|
||
|
|
|
||
|
|
import java.net.*;
|
||
|
|
import java.util.Enumeration;
|
||
|
|
|
||
|
|
public class Utility {
|
||
|
|
public static InetAddress getLocalAddress(){
|
||
|
|
try {
|
||
|
|
Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces();
|
||
|
|
while( b.hasMoreElements()){
|
||
|
|
for ( InterfaceAddress f : b.nextElement().getInterfaceAddresses())
|
||
|
|
if ( f.getAddress().isSiteLocalAddress())
|
||
|
|
return f.getAddress();
|
||
|
|
}
|
||
|
|
} catch (SocketException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void printExecption(Logger LOG, Exception e) {
|
||
|
|
String message = "";
|
||
|
|
|
||
|
|
for(StackTraceElement stackTraceElement : e.getStackTrace()) {
|
||
|
|
message = message + System.lineSeparator() + stackTraceElement.toString();
|
||
|
|
}
|
||
|
|
LOG.error("Exception: {}", e.getMessage());
|
||
|
|
LOG.error(message);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String getMacAddress() throws UnknownHostException,
|
||
|
|
SocketException {
|
||
|
|
InetAddress ipAddress = InetAddress.getLocalHost();
|
||
|
|
NetworkInterface networkInterface = NetworkInterface
|
||
|
|
.getByInetAddress(ipAddress);
|
||
|
|
byte[] macAddressBytes = networkInterface.getHardwareAddress();
|
||
|
|
StringBuilder macAddressBuilder = new StringBuilder();
|
||
|
|
|
||
|
|
for (int macAddressByteIndex = 0; macAddressByteIndex < macAddressBytes.length; macAddressByteIndex++) {
|
||
|
|
String macAddressHexByte = String.format("%02X",
|
||
|
|
macAddressBytes[macAddressByteIndex]);
|
||
|
|
macAddressBuilder.append(macAddressHexByte);
|
||
|
|
|
||
|
|
if (macAddressByteIndex != macAddressBytes.length - 1)
|
||
|
|
{
|
||
|
|
macAddressBuilder.append(":");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return macAddressBuilder.toString();
|
||
|
|
}
|
||
|
|
}
|