001import java.net.Inet4Address;
002import java.net.InetAddress;
003import java.net.NetworkInterface;
004import java.net.SocketException;
005import java.net.UnknownHostException;
006import java.util.Enumeration;
007 
008 
009public class Main{
010     
011    // 将byte[] 数组转换为16进制
012    public static String bytes2HexString(byte[] b) { 
013        String ret = ""
014        for (int i = 0; i < b.length; i++) { 
015            String hex = Integer.toHexString(b[ i ] & 0xFF); 
016        if (hex.length() == 1) { 
017            hex = '0' + hex; 
018        
019         ret += hex.toUpperCase(); 
020      
021      return ret; 
022    }  
023     
024    public static void main(String []args) throws UnknownHostException, SocketException{
025         
026        //Main.getIp();
027        Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
028        InetAddress ip = null;
029        while (allNetInterfaces.hasMoreElements())
030        {
031        NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
032        System.out.println(netInterface.getName());
033        Enumeration addresses = netInterface.getInetAddresses();
034        while (addresses.hasMoreElements())
035        {
036        ip = (InetAddress) addresses.nextElement();
037        if (ip != null && ip instanceof Inet4Address)
038        {
039        System.out.println("本机的IP = " + ip.getHostAddress());
040        }
041        }
042        }
043         
044         
045         
046    }
047     
048     
049     
050    public static byte[] getIp() throws UnknownHostException {
051 
052         byte[] b = InetAddress.getLocalHost().getAddress();
053 
054         Enumeration allNetInterfaces = null;
055 
056         try {
057 
058         allNetInterfaces = NetworkInterface.getNetworkInterfaces();
059 
060         } catch (SocketException e) {
061 
062         e.printStackTrace();
063 
064         }
065 
066         InetAddress ip = null;
067 
068         NetworkInterface netInterface = null;
069 
070         while (allNetInterfaces.hasMoreElements()) {
071 
072         netInterface = (NetworkInterface) allNetInterfaces.nextElement();
073 
074         if (netInterface.getName().trim().equals("eth0")){
075 
076         Enumeration addresses = netInterface.getInetAddresses();
077 
078         while (addresses.hasMoreElements()) {
079 
080         ip = (InetAddress) addresses.nextElement();
081 
082         }
083 
084         break;
085 
086         }
087 
088         }
089 
090         if (ip != null && ip instanceof Inet4Address) {
091 
092         return b = ip.getAddress();
093 
094         }
095 
096         return b;
097 
098         }
099     
100}
最近下载更多
1358849392  LV21 2024年4月12日
微信网友_6166561027215360  LV2 2022年10月8日
仰望星空five  LV1 2020年3月13日
skipple3  LV39 2020年3月3日
yang9999  LV1 2019年12月20日
lgp1991  LV1 2019年5月7日
快乐的男孩子  LV5 2019年1月23日
caohanren  LV11 2019年1月17日
Silence丶二哈  LV13 2019年1月16日
tian2019  LV1 2019年1月10日
最近浏览更多
1358849392  LV21 2024年4月12日
ming_123_9715  LV23 2022年12月15日
wanglun_wl  LV10 2022年10月18日
微信网友_6166561027215360  LV2 2022年10月8日
funcrit  LV2 2022年7月18日
zhy1989wz  LV7 2022年3月15日
农村拓哉 2021年12月7日
暂无贡献等级
13561143836  LV7 2021年11月10日
明天更美好  LV10 2021年9月9日
铁血战士  LV1 2021年6月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友