获取本机IP
public static String getLocalHostIp() throws SocketException
{
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
while(netInterfaces.hasMoreElements())
{
NetworkInterface ni = (NetworkInterface)netInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
while(inetAddresses.hasMoreElements())
{
InetAddress ip = inetAddresses.nextElement();
if(!ip.isLoopbackAddress())
{
return ip.getHostAddress();
}
}
}
return null;
}
获取本机MAC
public static String getLocalMac() throws IOException
{
Process p1 = Runtime.getRuntime().exec("ipconfig /all");
BufferedReader br = new BufferedReader(
new InputStreamReader(
p1
.getInputStream()));
String buf = br.readLine();
while(buf != null)
{
if(buf.contains("Physical Address"))
{
int i = buf.indexOf("Physical Address") + 36;
return buf.substring(i).trim();
}
buf = br.readLine();
}
return "000000";
}
|