- 浏览: 93381 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
grhglj:
能详细一点吗?都加到哪儿呀。
struts2.0中使用图形验证码 -
shingo7:
jotm哪来的2.3版
Spring引用Tomcat的 JTA事务 -
blogaaa:
这样配置完后,事务不能正常回滚,在同一个action中插入记录 ...
Spring引用Tomcat的 JTA事务
package test;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.StringTokenizer;
public final class NetworkUtils {
private final static int MACADDR_LENGTH = 17;
private final static String WIN_OSNAME = "Windows";
private final static String LINUX_OSNAME = "Linux";
private final static String OSX_OSNAME = "Mac OS X";
private final static String MACADDR_REG_EXP = "^[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}$";
private final static String WIN_MACADDR_EXEC = "ipconfig /all";
private final static String LINUX_MACADDR_EXEC = "ifconfig";
private final static String OSX_MACADDR_EXEC = "ifconfig";
public final static String getMacAddress() throws IOException{
String os = System.getProperty("os.name");
try{
if(os.startsWith(WIN_OSNAME)){
return windowsParseMacAddress(windowsIpConfigCommand());
}else if(os.startsWith(LINUX_OSNAME)){
return linuxParseMacAddress(linuxRunIfConfigCommand());
}else if(os.startsWith(OSX_OSNAME)) {
return osxParseMacAddress(osxRunIfConfigCommand());
}else{
throw new IOException("OS not supported : " + os);
}
}catch(Exception e){
e.printStackTrace();
throw new IOException(e.getMessage());
}
}
/**
* OSX stuff
*/
private final static String osxParseMacAddress(String ipConfigOutput) throws ParseException{
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch(java.net.UnknownHostException ex) {
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigOutput, "\n");
String lastMacAddress = null;
while(tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
boolean containsLocalHost = line.indexOf(localHost) >= 0;
//see if line contains IP address
if(containsLocalHost && lastMacAddress != null){
return lastMacAddress;
}
//see if line contains MAC address
int macAddressPosition = line.indexOf("ether");
if(macAddressPosition != 0) {
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
if(osxIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}
ParseException ex = new ParseException
("cannot read MAC address for " + localHost + " from [" + ipConfigOutput + "]", 0);
ex.printStackTrace();
throw ex;
}
private final static boolean osxIsMacAddress(String macAddressCandidate){
if(macAddressCandidate.length() != MACADDR_LENGTH){
return false;
}
if(!macAddressCandidate.matches(MACADDR_REG_EXP)){
return false;
}
return true;
}
private final static String osxRunIfConfigCommand() throws IOException{
Process p = Runtime.getRuntime().exec(OSX_MACADDR_EXEC);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for(;;){
int c = stdoutStream.read();
if(c == -1){
break;
}
buffer.append((char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/**
* Linux stuff
*/
private final static String linuxParseMacAddress(String ipConfigOutput) throws ParseException{
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ParseException(e.getMessage(),0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigOutput,"\n");
String lastMacAddress = null;
while(tokenizer.hasMoreTokens()){
String line = tokenizer.nextToken().trim();
boolean containsLocalHost = line.indexOf(localHost) >= 0;
//see if line contains IP address
if(containsLocalHost && lastMacAddress != null){
return lastMacAddress;
}
//see if line contains MAC address
int macAddressPosition = line.indexOf("HWaddr");
if(macAddressPosition <= 0){
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
if(linuxIsMacAddress(macAddressCandidate)){
lastMacAddress = macAddressCandidate;
continue;
}
}
ParseException ex = new ParseException("cannot read MAC address for " + localHost + " from [" + ipConfigOutput + "]",0);
ex.printStackTrace();
throw ex;
}
private final static boolean linuxIsMacAddress(String macAddressCandidate){
if(macAddressCandidate.length() != MACADDR_LENGTH){
return false;
}
if(!macAddressCandidate.matches(MACADDR_REG_EXP)){
return false;
}
return true;
}
private final static String linuxRunIfConfigCommand() throws IOException{
Process p = Runtime.getRuntime().exec(LINUX_MACADDR_EXEC);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for(;;){
int c = stdoutStream.read();
if(c == -1){
break;
}
buffer.append((char)c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/**
* Windows stuff
*/
private final static String windowsParseMacAddress(String ipConfigOutput) throws ParseException{
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ParseException(e.getMessage(),0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigOutput,"\n");
String lastMacAddress = null;
while(tokenizer.hasMoreTokens()){
String line = tokenizer.nextToken().trim();
//see if line contains IP address
if(line.endsWith(localHost) && lastMacAddress != null){
return lastMacAddress;
}
//see if line contains MAC address
int macAddressPosition = line.indexOf(":");
if(macAddressPosition <= 0){
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
if(windowsIsMacAddress(macAddressCandidate)){
lastMacAddress = macAddressCandidate;
continue;
}
}
ParseException ex = new ParseException("cannot read MAC address from [" + ipConfigOutput + "]",0);
ex.printStackTrace();
throw ex;
}
private final static boolean windowsIsMacAddress(String macAddressCandidate){
if(macAddressCandidate.length() != MACADDR_LENGTH){
return false;
}
if(!macAddressCandidate.matches(MACADDR_REG_EXP)){
return false;
}
return true;
}
private final static String windowsIpConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec(WIN_MACADDR_EXEC);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for(;;){
int c = stdoutStream.read();
if(c == -1){
break;
}
buffer.append((char)c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
System.out.println(" MAC ADDRESS");
System.out.println(" OS :" + System.getProperty("os.name"));
System.out.println(" IP/Localhost : " + InetAddress.getLocalHost().getHostAddress());
System.out.println(" MAC Address : " + getMacAddress());
}catch(Throwable t){
t.printStackTrace();
}
}
}
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.StringTokenizer;
public final class NetworkUtils {
private final static int MACADDR_LENGTH = 17;
private final static String WIN_OSNAME = "Windows";
private final static String LINUX_OSNAME = "Linux";
private final static String OSX_OSNAME = "Mac OS X";
private final static String MACADDR_REG_EXP = "^[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}$";
private final static String WIN_MACADDR_EXEC = "ipconfig /all";
private final static String LINUX_MACADDR_EXEC = "ifconfig";
private final static String OSX_MACADDR_EXEC = "ifconfig";
public final static String getMacAddress() throws IOException{
String os = System.getProperty("os.name");
try{
if(os.startsWith(WIN_OSNAME)){
return windowsParseMacAddress(windowsIpConfigCommand());
}else if(os.startsWith(LINUX_OSNAME)){
return linuxParseMacAddress(linuxRunIfConfigCommand());
}else if(os.startsWith(OSX_OSNAME)) {
return osxParseMacAddress(osxRunIfConfigCommand());
}else{
throw new IOException("OS not supported : " + os);
}
}catch(Exception e){
e.printStackTrace();
throw new IOException(e.getMessage());
}
}
/**
* OSX stuff
*/
private final static String osxParseMacAddress(String ipConfigOutput) throws ParseException{
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch(java.net.UnknownHostException ex) {
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigOutput, "\n");
String lastMacAddress = null;
while(tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
boolean containsLocalHost = line.indexOf(localHost) >= 0;
//see if line contains IP address
if(containsLocalHost && lastMacAddress != null){
return lastMacAddress;
}
//see if line contains MAC address
int macAddressPosition = line.indexOf("ether");
if(macAddressPosition != 0) {
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
if(osxIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}
ParseException ex = new ParseException
("cannot read MAC address for " + localHost + " from [" + ipConfigOutput + "]", 0);
ex.printStackTrace();
throw ex;
}
private final static boolean osxIsMacAddress(String macAddressCandidate){
if(macAddressCandidate.length() != MACADDR_LENGTH){
return false;
}
if(!macAddressCandidate.matches(MACADDR_REG_EXP)){
return false;
}
return true;
}
private final static String osxRunIfConfigCommand() throws IOException{
Process p = Runtime.getRuntime().exec(OSX_MACADDR_EXEC);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for(;;){
int c = stdoutStream.read();
if(c == -1){
break;
}
buffer.append((char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/**
* Linux stuff
*/
private final static String linuxParseMacAddress(String ipConfigOutput) throws ParseException{
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ParseException(e.getMessage(),0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigOutput,"\n");
String lastMacAddress = null;
while(tokenizer.hasMoreTokens()){
String line = tokenizer.nextToken().trim();
boolean containsLocalHost = line.indexOf(localHost) >= 0;
//see if line contains IP address
if(containsLocalHost && lastMacAddress != null){
return lastMacAddress;
}
//see if line contains MAC address
int macAddressPosition = line.indexOf("HWaddr");
if(macAddressPosition <= 0){
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
if(linuxIsMacAddress(macAddressCandidate)){
lastMacAddress = macAddressCandidate;
continue;
}
}
ParseException ex = new ParseException("cannot read MAC address for " + localHost + " from [" + ipConfigOutput + "]",0);
ex.printStackTrace();
throw ex;
}
private final static boolean linuxIsMacAddress(String macAddressCandidate){
if(macAddressCandidate.length() != MACADDR_LENGTH){
return false;
}
if(!macAddressCandidate.matches(MACADDR_REG_EXP)){
return false;
}
return true;
}
private final static String linuxRunIfConfigCommand() throws IOException{
Process p = Runtime.getRuntime().exec(LINUX_MACADDR_EXEC);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for(;;){
int c = stdoutStream.read();
if(c == -1){
break;
}
buffer.append((char)c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/**
* Windows stuff
*/
private final static String windowsParseMacAddress(String ipConfigOutput) throws ParseException{
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ParseException(e.getMessage(),0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigOutput,"\n");
String lastMacAddress = null;
while(tokenizer.hasMoreTokens()){
String line = tokenizer.nextToken().trim();
//see if line contains IP address
if(line.endsWith(localHost) && lastMacAddress != null){
return lastMacAddress;
}
//see if line contains MAC address
int macAddressPosition = line.indexOf(":");
if(macAddressPosition <= 0){
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
if(windowsIsMacAddress(macAddressCandidate)){
lastMacAddress = macAddressCandidate;
continue;
}
}
ParseException ex = new ParseException("cannot read MAC address from [" + ipConfigOutput + "]",0);
ex.printStackTrace();
throw ex;
}
private final static boolean windowsIsMacAddress(String macAddressCandidate){
if(macAddressCandidate.length() != MACADDR_LENGTH){
return false;
}
if(!macAddressCandidate.matches(MACADDR_REG_EXP)){
return false;
}
return true;
}
private final static String windowsIpConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec(WIN_MACADDR_EXEC);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for(;;){
int c = stdoutStream.read();
if(c == -1){
break;
}
buffer.append((char)c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
System.out.println(" MAC ADDRESS");
System.out.println(" OS :" + System.getProperty("os.name"));
System.out.println(" IP/Localhost : " + InetAddress.getLocalHost().getHostAddress());
System.out.println(" MAC Address : " + getMacAddress());
}catch(Throwable t){
t.printStackTrace();
}
}
}
发表评论
-
Drools与Spring集成 登录测试
2013-07-12 15:27 0转至: http://justsee.iteye.com/b ... -
Drools入门-----------环境搭建,分析Helloworld
2013-07-12 15:20 0转至: http://justsee.iteye.com/b ... -
H2学习
2013-07-12 15:11 0package test.h2; import ja ... -
BlockingQueue队列学习
2013-10-17 10:16 758package test; import java ... -
一个多线程计算器的实现
2013-07-12 14:56 0package test; import java.util ... -
JRobin Core学习
2012-12-04 14:29 4031原文地址: http://www.micmiu.com/ent ... -
Memcached学习——(四)
2012-11-28 17:11 0原文地址: http://snowolf.iteye.com/ ... -
Memcached学习——(三)
2012-11-28 17:10 0原文地址: http://snowolf ... -
Memcached学习——(二)
2012-11-28 17:08 970原文地址: http://snowolf.iteye.com/ ... -
Memcached学习——(一)
2012-11-28 17:07 1023原文地址: http://snowolf.iteye.com/ ... -
redis学习
2012-11-28 17:03 926原文地址:http://snowolf.iteye.com/b ... -
基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
2012-11-28 16:55 782原文地址:http://www.cnblogs.com/jif ... -
JAI处理TIFF格式图片
2012-09-17 17:48 13292懒得多说,直接代码了。。。。 import java.awt. ... -
全面掌握Java的异常处理机制
2009-03-05 17:17 964你觉得自己是一个Java专 ... -
java.lang包概述
2009-03-05 17:02 132014.1. 接口 java.lang.Appendab ... -
select下拉列表动态显示选择公司及部门信息——级联
2008-11-25 12:29 1280//=================公司类方法======= ... -
人民币大小写转
2008-11-25 12:26 1074String HanDigiStr[] = new Strin ... -
身份证号转换15位与18位
2008-11-25 12:24 1608//开始 15位到18位的身份证号转换 //身份证号码由 ... -
使ApplicationResources.properties支持中文
2008-11-25 12:15 1309使ApplicationResources.propertie ... -
利用XMLBean轻轻松松读写XML
2008-11-25 12:11 937一、关于XML解析 XML在Java应用程序里变得越来越 ...
相关推荐
刚刚发了一个获取电脑硬盘序列号的demo 想想还是把这个获取MAC地址的也发上来吧 请大家指点一二
在对网内主机进行监控时需要通过java获取远程机器的mac地址
java获取本机网卡的MAC地址,/** * 测试用的main方法. * * @param argc * 运行参数. */ public static void main(String[] argc) { String os = getOSName(); System.out.println(os); if(os....
主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下
Java/JSP获得客户端网卡MAC地址的三种方法解析
java获取Centos7服务器网卡ip 子网掩码 默认网关 DNS 同时设置网卡 及 重启网卡参考
网卡mac地址修改器 真正支持win10 不需重启即时生效 支持win7 8 10 网卡mac地址修改器 真正支持win10 不需重启即时生效 支持win7 8 10
//Java获得CPU序列号和网卡Mac地址 /* 利用Runtime call操作系统的命令,具体的命令取决于不同的操作系统,注意不要调用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])这个接口,...
NULL 博文链接:https://hylong05.iteye.com/blog/560600
很容易的就可以获得机器的MAC(网卡)地址,是用java语言编写的
java 程序, 亲测。 获取计算机的网卡IP地址。 runttime 的调用。
允许这个测试类,就可以轻松的获取到本地连接的mac网卡地址了,就可以作为设备的唯一编码。
C#程序源码,自己写的小工具,用于获取本机Mac地址,并对Mac地址进行AES加密及解密,已经过测试,方便使用。
java代码随机生成 00:18:34:6A:B2:67
很容易就可以获得 机器的MAC地址,我这使用java编写的。
从一个txt文件中导入一个网段内的计算机的名字,IP,MAC地址(这些是通过别的软件扫描出来的,我不懂做),然后再填写别的一些配置,就生成一个xml配置文件。客户端运行后会先读取本机的MAC地址,再根据该xml文件...
java.io.FileInputStream fis = new java.io.FileInputStream( new java.io.File("d:\\数据库.zip")); // 转换成网络输出流 java.net.ServerSocket ss = new java.net.ServerSocket(9000); java.net....
网上搜集了获取IP、MAC方法做了总结,提供多种获取IP、mac方式。支持获取多网卡IP、MAc获取
8.java简单的获取系统网卡mac地址 5 9. 增加tomcat的内存 6 10.JAVA获取系统当前的用户 7 11.javascript获取客户端网卡MAC地址和IP地址和计算机名 7 12.JAVA反射 8 13. 自定义ClassLoader 13 14. double保留两位数的...
Wifi连接网络,通过程序你将了解到定义WifiManager对象,定义WifiInfo对象,获取网络连接列表,定义一个WifiLock,打开Wifi网卡,关闭Wifi网卡,检查当前Wifi网卡状态,扫描周边网络,每次点击扫描之前清空上一次的...