java中怎么获取机器IP地址及MAC码
这篇文章将为大家详细讲解有关java中怎么获取机器IP地址及MAC码,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联公司拥有十载成都网站建设工作经验,为各大企业提供成都网站建设、网站制作服务,对于网页设计、PC网站建设(电脑版网站建设)、成都app软件开发公司、wap网站建设(手机版网站建设)、程序开发、网站优化(SEO优化)、微网站、域名注册等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了很多网站制作、网站设计、网络营销经验,集策划、开发、设计、营销、管理等网站化运作于一体,具备承接各种规模类型的网站建设项目的能力。
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: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午4:57:15
*/
public static String getLocalIP() {
String sIP = "";
InetAddress ip = null;
try {
boolean bFindIP = false;
Enumeration netInterfaces = (Enumeration) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
Enumeration 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: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午4:57:15
*/
public static List getLocalIPS() {
InetAddress ip = null;
List ipList = new ArrayList();
try {
Enumeration netInterfaces = (Enumeration) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
Enumeration 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: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午1:27:25
*/
public static String getMacId() {
String macId = "";
InetAddress ip = null;
NetworkInterface ni = null;
try {
boolean bFindIP = false;
Enumeration netInterfaces = (Enumeration) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
ni = (NetworkInterface) netInterfaces
.nextElement();
// ----------特定情况,可以考虑用ni.getName判断
// 遍历所有ip
Enumeration 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: zhangyang33@sinopharm.com
* @version: 2014年9月5日 下午1:27:25
*/
public static List getMacIds() {
InetAddress ip = null;
NetworkInterface ni = null;
List macList = new ArrayList();
try {
Enumeration netInterfaces = (Enumeration) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
ni = (NetworkInterface) netInterfaces
.nextElement();
// ----------特定情况,可以考虑用ni.getName判断
// 遍历所有ip
Enumeration 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码
网页网址:http://www.jxjierui.cn/article/jjjdpo.html


咨询
建站咨询
