java如何查询本机ip地址和mac地址

如题所述

     Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:

import java.net.*;
public class NetInfo {
 public static void main(String[] args) {
    new NetInfo().say();
    }
 public void say() {
   try {
   InetAddress i = InetAddress.getLocalHost();
   System.out.println(i);                  //计算机名称和IP
   System.out.println(i.getHostName());    //名称
   System.out.println(i.getHostAddress()); //只获得IP
   }
   catch(Exception e){e.printStackTrace();}
 }
}

      也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-06-22
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class getIpAndMac {

public static void main(String[] args) throws UnknownHostException,
SocketException {
InetAddress addr = InetAddress.getLocalHost();
String ip = addr.getHostAddress().toString();// 获得本机IP
System.out.println("本机ip:" + ip);
getLocalMac(addr);
}

private static void getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
// 获取网卡,获取地址
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// 字节转换为整数
int temp = mac[i] & 0xff;
String str = Integer.toHexString(temp);
if (str.length() == 1) {
sb.append("0" + str);
} else {
sb.append(str);
}
}
System.out.println("本机MAC地址:" + sb.toString().toUpperCase());
}
}

第2个回答  2015-07-05
import java.io.IOException;  
import java.io.InputStreamReader;  
  
  
public class GetMacAddress {  
  
          
     public static String getOSName() {    
         return System.getProperty("os.name").toLowerCase();    
     }    
             
         
     public static String getUnixMACAddress() {    
         String mac = null;    
         BufferedReader bufferedReader = null;    
         Process process = null;    
         try {    
                 
             process = Runtime.getRuntime().exec("ifconfig eth0");  
             bufferedReader = new BufferedReader(new InputStreamReader(process    
                     .getInputStream()));    
             String line = null;    
             int index = -1;    
             while ((line = bufferedReader.readLine()) != null) {    
                      
                 index = line.toLowerCase().indexOf("hwaddr");   
                      
                 if (index != -1) {    
                          
                     mac = line.substring(index +"hwaddr".length()+ 1).trim();  
                     break;    
                 }    
             }    
         } catch (IOException e) {    
             e.printStackTrace();    
         } finally {    
             try {    
                 if (bufferedReader != null) {    
                     bufferedReader.close();    
                 }    
             } catch (IOException e1) {    
                e1.printStackTrace();    
            }    
             bufferedReader = null;    
             process = null;    
         }    
     
         return mac;    
     }    
         
           
           
             
         public static String getLinuxMACAddress() {    
             String mac = null;    
             BufferedReader bufferedReader = null;    
             Process process = null;    
             try {    
                     
                 process = Runtime.getRuntime().exec("ifconfig eth0");  
                 bufferedReader = new BufferedReader(new InputStreamReader(process    
                         .getInputStream()));    
                 String line = null;    
                 int index = -1;    
                 while ((line = bufferedReader.readLine()) != null) {    
                     index = line.toLowerCase().indexOf("硬件地址");   
                          
                     if (index != -1) {    
                              
                         mac = line.substring(index+4).trim();  
                         break;    
                     }    
                 }    
             } catch (IOException e) {    
                 e.printStackTrace();    
             } finally {    
                 try {    
                     if (bufferedReader != null) {    
                         bufferedReader.close();    
                     }    
                 } catch (IOException e1) {    
                    e1.printStackTrace();    
                }    
                 bufferedReader = null;    
                 process = null;    
             }  
         
             return mac;    
         }  
           
             
         public static String getWindowsMACAddress() {    
             String mac = null;    
             BufferedReader bufferedReader = null;    
             Process process = null;    
             try {    
                     
                 process = Runtime.getRuntime().exec("ipconfig /all");  
                 bufferedReader = new BufferedReader(new InputStreamReader(process    
                         .getInputStream()));    
                 String line = null;    
                 int index = -1;    
                 while ((line = bufferedReader.readLine()) != null) {    
                          
                     index = line.toLowerCase().indexOf("physical address");   
                     if (index != -1) {  
                         index = line.indexOf(":");  
                         if (index != -1) {  
                                  
                            mac = line.substring(index + 1).trim();   
                        }  
                         break;    
                     }  
                 }  
             } catch (IOException e) {    
                 e.printStackTrace();    
             }finally {    
                 try {    
                     if (bufferedReader != null) {    
                         bufferedReader.close();    
                       }    
                 }catch (IOException e1) {    
                     e1.printStackTrace();    
                   }    
                 bufferedReader = null;    
                 process = null;    
             }    
         
             return mac;    
         }    
         
             
         public static void main(String[] argc) {    
             String os = getOSName();    
             System.out.println(os);    
             if(os.startsWith("windows")){    
                 String mac = getWindowsMACAddress();    
                 System.out.println("本地是windows:"+mac);    
             }else if(os.startsWith("linux")){    
                   String mac = getLinuxMACAddress();    
                 System.out.println("本地是Linux系统,MAC地址是:"+mac);  
             }else{    
                 String mac = getUnixMACAddress();                        
                 System.out.println("本地是Unix系统 MAC地址是:"+mac);  
             }    
         }    
  
}

第3个回答  2012-05-06
IP地址:java.net.InetAddress类的 getLocalHost() 方法
MAC地址:没有直接的方法,百度搜“java 获得本机mac地址”有很多。
我的理解:因为java的网络程序是面向TCP/IP层的,MAC地址属于链路层了,在IP层的下面,java看不到。 所以要调用c和c++做的程序,比如Windows系统自带的ipconfig
第4个回答  2015-07-22
import java.net.*;
public class NetInfo {
public static void main(String[] args) {
new NetInfo().say();
}
public void say() {
try {
InetAddress i = InetAddress.getLocalHost();
System.out.println(i); //计算机名称和IP
System.out.println(i.getHostName()); //名称
System.out.println(i.getHostAddress()); //只获得IP
}
catch(Exception e){e.printStackTrace();}
}
}