Java获取本机的IP与MAC地址_Java设置代理
2017-12-05 14:02:50  By: shinyuu

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


获取mac地址

// 获取mac地址
public static String getMacAddress() {
    try {
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        byte[] mac = null;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
            if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
                continue;
            } else {
                mac = netInterface.getHardwareAddress();
                if (mac != null) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mac.length; i  ) {
                        sb.append(String.format("X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                    }
                    if (sb.length() > 0) {
                        return sb.toString();
                    }
                }
            }
        }
    } catch (Exception e) {
        _logger.error("MAC地址获取失败", e);
    }
    return "";
}


获取ip地址

// 获取ip地址
public static String getIpAddress() {
    try {
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
            if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
                continue;
            } else {
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address) {
                        return ip.getHostAddress();
                    }
                }
            }
        }
    } catch (Exception e) {
        _logger.error("IP地址获取失败", e);
    }
    return "";
}


以上的代码中netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !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访问代理
/**
 * 设置http代理
 */
public static void setHttpProxy() {
    Properties prop = System.getProperties();
    // 设置http访问要使用的代理服务器的地址
    prop.setProperty("http.proxyHost", HTTP_PROXY_HOST);
    // 设置http访问要使用的代理服务器的端口
    prop.setProperty("http.proxyPort", HTTP_PROXY_PORT);
    // 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
    prop.setProperty("http.nonProxyHosts", RemoteConfig.PROXT_FILTER_DOMAIN);
}

/**
 * 移除http代理
 */
public static void removeHttpProxy() {
    Properties prop = System.getProperties();
    prop.remove("http.proxyHost");
    prop.remove("http.proxyPort");
    prop.remove("http.nonProxyHosts");
}


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

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


若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力

想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)

或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)

如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教

为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)

感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛


快速评论


技术评论

  • 该技术还没有评论、赶快抢沙发吧...
DD记账
top
+