|
|
|
@ -5,8 +5,13 @@ import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.net.*;
|
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
/**
|
|
|
|
|
* IP地址
|
|
|
|
|
*
|
|
|
|
|
* @Author xhj
|
|
|
|
@ -53,5 +58,86 @@ public class IPUtils {
|
|
|
|
|
|
|
|
|
|
return ip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getMACAddress(String ip) {
|
|
|
|
|
String str = "";
|
|
|
|
|
String macAddress = "";
|
|
|
|
|
// final String LOOPBACK_ADDRESS = "127.0.0.1";
|
|
|
|
|
final String LOOPBACK_ADDRESS = getLocalIP();
|
|
|
|
|
// final String LOOPBACK_ADDRESS = InetAddress.getLocalHost().getHostAddress();
|
|
|
|
|
// final String LOOPBACK_ADDRESS_ipv6="0:0:0:0:0:0:0:1";
|
|
|
|
|
// 如果为127.0.0.1,则获取本地MAC地址。
|
|
|
|
|
if (LOOPBACK_ADDRESS.equals(ip)) {
|
|
|
|
|
try {
|
|
|
|
|
InetAddress inetAddress = InetAddress.getLocalHost();
|
|
|
|
|
// 貌似此方法需要JDK1.6。
|
|
|
|
|
byte[] mac = NetworkInterface.getByInetAddress(inetAddress)
|
|
|
|
|
.getHardwareAddress();
|
|
|
|
|
// 下面代码是把mac地址拼装成String
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
for (int i = 0; i < mac.length; i++) {
|
|
|
|
|
if (i != 0) {
|
|
|
|
|
sb.append("-");
|
|
|
|
|
}
|
|
|
|
|
// mac[i] & 0xFF 是为了把byte转化为正整数
|
|
|
|
|
String s = Integer.toHexString(mac[i] & 0xFF);
|
|
|
|
|
sb.append(s.length() == 1 ? 0 + s : s);
|
|
|
|
|
}
|
|
|
|
|
// 把字符串所有小写字母改为大写成为正规的mac地址并返回
|
|
|
|
|
macAddress = sb.toString().trim().toUpperCase();
|
|
|
|
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return macAddress;
|
|
|
|
|
} else {
|
|
|
|
|
// 获取非本地IP的MAC地址
|
|
|
|
|
try {
|
|
|
|
|
// System.out.println(ip);
|
|
|
|
|
Process p = Runtime.getRuntime()
|
|
|
|
|
.exec("nbtstat -A " + ip);
|
|
|
|
|
System.out.println("===process=="+p);
|
|
|
|
|
InputStreamReader ir = new InputStreamReader(p.getInputStream());
|
|
|
|
|
|
|
|
|
|
BufferedReader br = new BufferedReader(ir);
|
|
|
|
|
|
|
|
|
|
while ((str = br.readLine()) != null) {
|
|
|
|
|
if(str.indexOf("MAC")>1){
|
|
|
|
|
macAddress = str.substring(str.indexOf("MAC")+9, str.length());
|
|
|
|
|
macAddress = macAddress.trim();
|
|
|
|
|
System.out.println("macAddress:" + macAddress);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
p.destroy();
|
|
|
|
|
br.close();
|
|
|
|
|
ir.close();
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
}
|
|
|
|
|
return macAddress;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static String getLocalIP(){
|
|
|
|
|
String localip="";
|
|
|
|
|
try {
|
|
|
|
|
Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
|
|
|
|
|
while (nifs.hasMoreElements()) {
|
|
|
|
|
NetworkInterface nif = nifs.nextElement();
|
|
|
|
|
|
|
|
|
|
// 获得与该网络接口绑定的 IP 地址,一般只有一个
|
|
|
|
|
Enumeration<InetAddress> addresses = nif.getInetAddresses();
|
|
|
|
|
while (addresses.hasMoreElements()) {
|
|
|
|
|
InetAddress addr = addresses.nextElement();
|
|
|
|
|
|
|
|
|
|
if (addr instanceof Inet4Address && nif.getName().equals("wlan1")) { // 只关心 IPv4 地址
|
|
|
|
|
|
|
|
|
|
System.out.println("网卡接口名称:" + nif.getName());
|
|
|
|
|
System.out.println("网卡接口地址:" + addr.getHostAddress());
|
|
|
|
|
localip=addr.getHostAddress();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}catch (Exception e){}
|
|
|
|
|
return localip;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|