Java获取本机的IP与MAC地址

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java获取本机的IP与MAC地址相关的知识,希望对你有一定的参考价值。

    有些机器有许多虚拟的网卡,获取IP地址时会出现一些意外,所以需要一些验证:

 1     // 获取mac地址
 2     public static String getMacAddress() {
 3         try {
 4             Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
 5             byte[] mac = null;
 6             while (allNetInterfaces.hasMoreElements()) {
 7                 NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
 8                 if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
 9                     continue;
10                 } else {
11                     mac = netInterface.getHardwareAddress();
12                     if (mac != null) {
13                         StringBuilder sb = new StringBuilder();
14                         for (int i = 0; i < mac.length; i++) {
15                             sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
16                         }
17                         if (sb.length() > 0) {
18                             return sb.toString();
19                         }
20                     }
21                 }
22             }
23         } catch (Exception e) {
24             _logger.error("MAC地址获取失败", e);
25         }
26         return "";
27     }
28 
29     // 获取ip地址
30     public static String getIpAddress() {
31         try {
32             Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
33             InetAddress ip = null;
34             while (allNetInterfaces.hasMoreElements()) {
35                 NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
36                 if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
37                     continue;
38                 } else {
39                     Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
40                     while (addresses.hasMoreElements()) {
41                         ip = addresses.nextElement();
42                         if (ip != null && ip instanceof Inet4Address) {
43                             return ip.getHostAddress();
44                         }
45                     }
46                 }
47             }
48         } catch (Exception e) {
49             _logger.error("IP地址获取失败", e);
50         }
51         return "";
52     }

    以上的代码中

netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()

    能很好地把一些非物理网卡或无用网上过滤掉,然后再取网上的IPV4地址即可。

 

    说到这里,还有一些常用的:

    1、获取当前机器的操作系统

    public final static String WIN_OS = "WINDOWS";
    public final static String MAC_OS = "MAC";
    public final static String LINUX_OS = "LINUX";
    public final static String OTHER_OS = "OTHER";

    public static String getOS() {
        if (SystemUtils.IS_OS_WINDOWS){
            return WIN_OS;
        }
        if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX){
            return MAC_OS;
        }
        if (SystemUtils.IS_OS_UNIX){
            return LINUX_OS;
        }
        return OTHER_OS;
    }

     2、设置HTTP访问代理

 1     /**
 2      * 设置http代理
 3      */
 4     public static void setHttpProxy() {
 5         Properties prop = System.getProperties();
 6         // 设置http访问要使用的代理服务器的地址
 7         prop.setProperty("http.proxyHost", HTTP_PROXY_HOST);
 8         // 设置http访问要使用的代理服务器的端口
 9         prop.setProperty("http.proxyPort", HTTP_PROXY_PORT);
10         // 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
11         prop.setProperty("http.nonProxyHosts", RemoteConfig.PROXT_FILTER_DOMAIN);
12     }
13     
14     /**
15      * 移除http代理
16      */
17     public static void removeHttpProxy() {
18         Properties prop = System.getProperties();
19         prop.remove("http.proxyHost");
20         prop.remove("http.proxyPort");
21         prop.remove("http.nonProxyHosts");
22     }

    在应用启动时,访问HTTP请求前,设置好就行。当然,http.nonProxyHosts可以不用设置,表示所有的HTTP请求都走代理。

    至于HTTPS代理,类似可以这样设置:

System.setProperty("https.proxyHost", "HTTP_PROXY_HOST");
System.setProperty(
"https.proxyPort", "HTTP_PROXY_PORT");

 

    转载请注明原址:http://www.cnblogs.com/lekko/p/5888962.html

以上是关于Java获取本机的IP与MAC地址的主要内容,如果未能解决你的问题,请参考以下文章

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

java获取本机IP地址和MAC地址的方法

java 怎么获取本机ip地址

Linux中用shell编程获取IP地址及MAC地址

如何在Node.js中获取本机本地IP地址

Python根据IP地址获取MAC地址