// <summary>
/// 获取本地IP地址
/// </summary>
/// <returns></returns>
private string GetClientIP()
{
String localMac = null;
try {
InetAddress inetAddress = InetAddress.getLocalHost();
/**
* 获取电脑网卡的AMC地址
* 返回包含硬件地址的 byte 数组;如果地址不存在或不可访问,则返回 null
* 如果电脑因为网卡被禁用,则这里获取会返回 null
*/
byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
if (mac == null) {
return null;
}
StringBuffer stringBuffer = new StringBuffer("");
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
stringBuffer.append("-");
}
/**
* 转换mac的字节数组
*/
int temp = mac[i] & 0xff;
String str = Integer.toHexString(temp);
if (str.length() == 1) {
stringBuffer.append("0" + str);
} else {
stringBuffer.append(str);
}
}
localMac = stringBuffer.toString().toUpperCase();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return localMac;
InetAddress inetAddress = InetAddress.getLocalHost();
/**
* 获取电脑网卡的AMC地址
* 返回包含硬件地址的 byte 数组;如果地址不存在或不可访问,则返回 null
* 如果电脑因为网卡被禁用,则这里获取会返回 null
*/
byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
if (mac == null) {
return null;
}
StringBuffer stringBuffer = new StringBuffer("");
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
stringBuffer.append("-");
}
/**
* 转换mac的字节数组
*/
int temp = mac[i] & 0xff;
String str = Integer.toHexString(temp);
if (str.length() == 1) {
stringBuffer.append("0" + str);
} else {
stringBuffer.append(str);
}
}
localMac = stringBuffer.toString().toUpperCase();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return localMac;
}