获取真实mac地址

获取已的网卡的mac地址(方法一)

package wingsoft.custom.tools;

import java.io.PrintStream;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

@SuppressWarnings("unused")
public class MacUtil
{
public static String getMacAddress()
{
try
{
Enumeration<NetworkInterface> ni = NetworkInterface.getNetworkInterfaces();
while (ni.hasMoreElements())
{
NetworkInterface netI = (NetworkInterface)ni.nextElement();

byte[] bytes = netI.getHardwareAddress();
if ((netI.isUp()) && (netI != null) && (bytes != null) &&
(bytes.length == 6))
{
StringBuffer sb = new StringBuffer();
for (byte b : bytes)
{
sb.append(Integer.toHexString((b & 0xF0) >> 4));

sb.append(Integer.toHexString(b & 0xF));
sb.append("-");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString().toUpperCase();
}
}
}
catch (SocketException e)
{
e.printStackTrace();
}
return null;
}

public static void main(String[] args)
{
System.out.println(getMacAddress());
}
}

利用cmd命令获取mac地址(方法二)

package wingsoft.custom;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

//import wingsoft.custom.tools.MacUtil;
import wingsoft.tool.db.ConnectionPool;
import wingsoft.tool.db.ConnectionPoolManager;

public class toolTip
{
private InputStream responseText;

public void setResponseText(InputStream responseText)
{
this.responseText = responseText;
}

public InputStream getResponseText()
{
return this.responseText;
}

public String getToolTip()
{
HttpServletRequest request = ServletActionContext.getRequest();
String ipadr =request.getRemoteAddr();
System.out.println(request.getRemoteAddr());
String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -a " + ipadr);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
//if (str.indexOf("MAC Address") > 1) {
if (str.indexOf("MAC") > 1) {
macAddress = str.substring(
str.indexOf("=") + 2, str.length());
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}

String lab_no = request.getParameter("v_lab_no");
System.out.println(lab_no);

String userid = request.getParameter("v_userid");
System.out.println(userid);

ConnectionPool pool = ConnectionPoolManager.getPool("CMServer");
System.out.println(pool);
Connection conn = null;

PreparedStatement ps = null;
ResultSet rs = null;
String sql = "";

StringBuffer dataMap = new StringBuffer();
try
{
conn = pool.getConnection();
//String mac = MacUtil.getMacAddress();
sql = "select l_login_id.nextval from dual";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
rs.next();
int login_id = rs.getInt(1);

sql = " insert into l_lab_mac_temp(id,mac,ip,lab_no,member_no) values("+login_id+",'" + macAddress + "','"+ipadr+"','"+lab_no+"','"+userid+"')";
System.out.println("print----sql:" + sql);
ps = conn.prepareStatement(sql);
ps.execute(sql);
dataMap.append("[{msg:"success",loginID:""+login_id+ ""}]");
System.out.println(dataMap.toString());
setResponseText(new ByteArrayInputStream(dataMap.toString()
.getBytes("utf-8")));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (ps != null) {
ps.close();
}
}

catch (SQLException e)
{
e.printStackTrace();
}
pool.returnConnection(conn);
dataMap.append("[{msg:"failed"}]");
}
return "textPlain";
}
}

原文地址:https://www.cnblogs.com/devin818/p/5090796.html