java百分百获取到机器IP地址及MAC码
已测系统:
windows linux unix
排除127.0.0.1 和 0.0.0.0.1等非正常IP
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; public class IpUtil { private IpUtil(){} /** * 此方法描述的是:获得服务器的IP地址 * @author: jdkleo * @version: 2014年9月5日 下午4:57:15 */ public static String getLocalIP() { String sIP = ""; InetAddress ip = null; try { boolean bFindIP = false; Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { if (bFindIP) { break; } NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (!ip.isLoopbackAddress() && ip.getHostAddress().matches( "(\\d{1,3}\\.){3}\\d{1,3}")) { bFindIP = true; break; } } } } catch (Exception e) { OutUtil.error(IpUtil.class, e.getMessage()); } if (null != ip) { sIP = ip.getHostAddress(); } return sIP; } /** * 此方法描述的是:获得服务器的IP地址(多网卡) * @author: jdkleo * @version: 2014年9月5日 下午4:57:15 */ public static List<String> getLocalIPS() { InetAddress ip = null; List<String> ipList = new ArrayList<String>(); try { Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (!ip.isLoopbackAddress() && ip.getHostAddress().matches( "(\\d{1,3}\\.){3}\\d{1,3}")) { ipList.add(ip.getHostAddress()); } } } } catch (Exception e) { OutUtil.error(IpUtil.class, e.getMessage()); } return ipList; } /** * 此方法描述的是:获得服务器的MAC地址 * @author: jdkleo * @version: 2014年9月5日 下午1:27:25 */ public static String getMacId() { String macId = ""; InetAddress ip = null; NetworkInterface ni = null; try { boolean bFindIP = false; Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { if (bFindIP) { break; } ni = (NetworkInterface) netInterfaces .nextElement(); // ----------特定情况,可以考虑用ni.getName判断 // 遍历所有ip Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (!ip.isLoopbackAddress() // 非127.0.0.1 && ip.getHostAddress().matches( "(\\d{1,3}\\.){3}\\d{1,3}")) { bFindIP = true; break; } } } } catch (Exception e) { OutUtil.error(IpUtil.class, e.getMessage()); } if (null != ip) { try { macId = getMacFromBytes(ni.getHardwareAddress()); } catch (SocketException e) { OutUtil.error(IpUtil.class, e.getMessage()); } } return macId; } /** * 此方法描述的是:获得服务器的MAC地址(多网卡) * @author: jdkleo * @version: 2014年9月5日 下午1:27:25 */ public static List<String> getMacIds() { InetAddress ip = null; NetworkInterface ni = null; List<String> macList = new ArrayList<String>(); try { Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { ni = (NetworkInterface) netInterfaces .nextElement(); // ----------特定情况,可以考虑用ni.getName判断 // 遍历所有ip Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (!ip.isLoopbackAddress() // 非127.0.0.1 && ip.getHostAddress().matches( "(\\d{1,3}\\.){3}\\d{1,3}")) { macList.add(getMacFromBytes(ni.getHardwareAddress())); } } } } catch (Exception e) { OutUtil.error(IpUtil.class, e.getMessage()); } return macList; } private static String getMacFromBytes(byte[] bytes) { StringBuffer mac = new StringBuffer(); byte currentByte; boolean first = false; for (byte b : bytes) { if (first) { mac.append("-"); } currentByte = (byte) ((b & 240) >> 4); mac.append(Integer.toHexString(currentByte)); currentByte = (byte) (b & 15); mac.append(Integer.toHexString(currentByte)); first = true; } return mac.toString().toUpperCase(); } }
相关推荐
主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下
获取客户端ip地址( 这个必须从客户端传到后台): jsp页面下,很简单,request....因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换java 获取客户端ip mac地址
获取用户请求IP地址 根据IP地址获取mac地址
在对网内主机进行监控时需要通过java获取远程机器的mac地址
Java,运用了多线程内部执行,不影响其他登录验证操作。在Controller类获取客户端的IP地址的方法是:request....所以我们这里添加了条件验证,将客户端IP及Mac地址(同网段)与本地本地IP及Mac地址方法分开。
java 获取计算机名与IP地址 java 获取计算机名与IP地址 java 获取计算机名与IP地址
java中通过ip获取远程客户端的mac地址,支持跨平台
Java获取客户端真实IP地址
Java获取本机MAC地址IP地址主机名
获取计算机的mac地址和ip地址,mac地址返回的是一个list,ip地址是string
NULL 博文链接:https://fanshuyao.iteye.com/blog/2436489
此java类获取的是公网的IP地址,如想获取本机IP但不是公网IP查看我其他资源。
Android获取手机ip地址及mac地址
可以通过java获取客户端IPMAC主机名等信息。下载就能用,欢迎下载!
本实验利用WinPcap实现ARP协议,从而获取以太网上任意一台主机的IP地址与MAC地址的对应关系。
主要介绍了java实现根据ip地址获取地理位置的代码分享,本文中使用的是QQ在线接口,也可以使用新浪、淘宝等提供的在线接口,需要的朋友可以参考下
java获取客户端ip及mac地址方法,适合web开发者
java 获取网址的IP地址 java 获取网址的IP地址 java 获取网址的IP地址
此JAVA程序能够轻松获取别人的ip地址,非常好用。。。。
LINUX编程,通过IP地址获取MAC地址代码。方