- 浏览: 95729 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
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 806package test; import java ... -
一个多线程计算器的实现
2013-07-12 14:56 0package test; import java.util ... -
JRobin Core学习
2012-12-04 14:29 4141原文地址: 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 1007原文地址: http://snowolf.iteye.com/ ... -
Memcached学习——(一)
2012-11-28 17:07 1131原文地址: http://snowolf.iteye.com/ ... -
redis学习
2012-11-28 17:03 968原文地址:http://snowolf.iteye.com/b ... -
基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
2012-11-28 16:55 831原文地址:http://www.cnblogs.com/jif ... -
JAI处理TIFF格式图片
2012-09-17 17:48 13330懒得多说,直接代码了。。。。 import java.awt. ... -
全面掌握Java的异常处理机制
2009-03-05 17:17 1032你觉得自己是一个Java专 ... -
java.lang包概述
2009-03-05 17:02 133464.1. 接口 java.lang.Appendab ... -
select下拉列表动态显示选择公司及部门信息——级联
2008-11-25 12:29 1329//=================公司类方法======= ... -
人民币大小写转
2008-11-25 12:26 1153String HanDigiStr[] = new Strin ... -
身份证号转换15位与18位
2008-11-25 12:24 1631//开始 15位到18位的身份证号转换 //身份证号码由 ... -
使ApplicationResources.properties支持中文
2008-11-25 12:15 1338使ApplicationResources.propertie ... -
利用XMLBean轻轻松松读写XML
2008-11-25 12:11 969一、关于XML解析 XML在Java应用程序里变得越来越 ...
相关推荐
在Java编程中,获取计算机硬件信息是常见的需求之一,其中之一就是获取网卡(Network Interface Card,简称NIC)的物理地址,也就是MAC地址。MAC地址是一个唯一标识网络接口的硬件地址,通常由六组两进制数字组成,...
JAVA 获取网卡物理地址(Windows 和 Linux) 在 Java 中获取网卡物理地址是一项常见的需求,特别是在开发网络相关的应用程序时。网卡物理地址,也称为 MAC 地址,是一个唯一的识别码,用来标识网络设备。今天,我们...
本文将深入解析在Java/JSP环境中获取客户端网卡MAC地址的三种常见方法,旨在为开发者提供全面而深入的理解。 ### 方法一:通过Java NIO(非阻塞I/O)库 Java NIO库提供了访问底层网络接口的能力,从而可以获取包括...
### Java 获取客户端 IP 和 MAC 地址 在Java Web开发中,有时我们需要获取客户端的IP地址甚至是MAC地址,以便进行安全验证、用户行为追踪等操作。本文将详细介绍如何通过Java来实现这一功能。 #### 一、获取客户端...
获取网卡MAC地址是网络管理、设备识别或者安全验证等场景中的常见需求。在不同的操作系统中,获取MAC地址的方法略有不同。以下是一些主要操作系统的获取方法: 1. **Windows系统**: - 命令行:通过运行`cmd`打开...
在对网内主机进行监控时需要通过java获取远程机器的mac地址
在Java编程中,获取服务器的IP地址和MAC地址是常见的需求,特别是在网络通信和系统管理中。本篇文章将详细介绍如何使用Java实现这一功能,包括针对单网卡和多网卡环境的情况。 首先,获取服务器的IP地址通常涉及到`...
在Java编程环境中,获取网络接口(网卡)的地址是一项常见的任务,特别是在系统级编程或者网络通信的应用中。本文将详细讲解如何利用J2SE(Java 2 Standard Edition)中的`ProcessBuilder`类来实现这一功能,特别是...
### Java 获取本机网卡的MAC地址 在Java开发中,有时我们需要获取当前计算机的MAC地址,例如在网络设备管理或安全认证等场景中。本文将详细介绍如何通过Java代码获取本机网卡的MAC地址,并针对不同的操作系统(如...
Java 中获取 IP 地址、主机名称、网卡地址 Java 中获取 IP 地址、主机名称、网卡地址是 NETWORK 编程中的一个重要主题。在 Java 中,我们可以使用 InetAddress 类和 Runtime 类来获取这些信息。下面我们来详细介绍...
java获取Centos7服务器网卡ip 子网掩码 默认网关 DNS 同时设置网卡 及 重启网卡参考
在Java编程语言中,获取计算机硬件的物理地址,即网卡MAC地址,是常见的系统信息获取需求。MAC地址是一个用于标识网络设备的唯一物理地址,通常由六组两字符的十六进制数字组成,例如“00:14:22:01:23:45”。本篇...
标题提到的“网卡MAC地址修改器”是一款软件工具,专为Windows操作系统设计,特别是针对Windows 10系统。它声称能够方便地修改网卡的MAC地址,并且在修改后无需重启计算机即可立即生效。这在传统的操作系统中通常...
本文将详细讲解如何在不同的操作系统中获取网卡的MAC地址。 ### Windows系统 在Windows中,可以通过命令行工具`cmd.exe`来获取MAC地址: 1. 打开命令提示符:按下`Win + R`,输入`cmd`,然后点击“确定”或按回车...
- **校验物理唯一性**:如果需要确保MAC地址在实际网络中是唯一的,可以查询网络接口或使用第三方库,如`java.net.NetworkInterface`类来获取和验证。 3. **MAC地址转换**: - **二进制与十六进制转换**:可以...
- **Java**:使用`java.net.NetworkInterface`类,遍历所有网络接口并获取MAC地址。 - **C#**:通过`ManagementClass`和`ManagementObjectCollection`操作WMI。 3. **第三方软件**:有些系统工具或网络诊断软件也...
在Linux和Windows环境下,获取本机所有网卡MAC地址的方法略有不同。在Linux中,可以通过`ifconfig`命令或`/sys/class/net`目录下的文件系统接口来获取;而在Windows中,可以使用WMI(Windows Management ...
- **Java**:使用`java.net.NetworkInterface`类,遍历所有网络接口并获取MAC地址。 3. 系统管理工具 大多数操作系统都提供了图形界面的网络设置工具,用户可以在其中查看设备的MAC地址: - **Windows**:控制...
Java提供了更直接的方式来获取MAC地址,主要通过`java.net.NetworkInterface`类。以下是一个简单的Java示例,展示了如何在Windows和Linux上获取MAC地址: ```java import java.net.NetworkInterface; import java...
这篇博客文章可能详细介绍了如何利用Jpcap库来获取网卡地址、数据源地址以及如何捕获和分析网络数据包。下面将对这些知识点进行深入探讨。 1. **网卡地址**: - 在计算机网络中,每个网卡都有一个唯一的物理地址,...