轉帖|其它|編輯:郝浩|2010-11-05 15:55:11.000|閱讀 1158 次
概述:本文主要介紹JSF/JAVA根據IP獲取客戶端Mac地址,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
需要對用戶的 ip 和 mac 地址進行驗證,這里用到獲取客戶端ip和mac地址的兩個方法,留存。
1.獲取客戶端ip地址( 這個必須從客戶端傳到后臺):
jsp頁面下,很簡單,request.getRemoteAddr() ;
因為系統的VIew層是用JSF來實現的,因此頁面上沒法直接獲得類似request,在bean里做了個強制轉換
Java代碼
public String getMyIP() {
try {
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest();
return request.getRemoteAddr();
}
catch (Exception e) {
e.printStackTrace();
}
return "";
}
2.獲取客戶端mac地址
調用window的命令,在后臺Bean里實現 通過ip來獲取mac地址。方法如下:
代碼
public String getMACAddress(String ip){
String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
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) {
macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}
完整代碼:
Java代碼
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class GetMACAddress {
public String getMACAddress(String ipAddress) {
String str = "", strMAC = "", macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.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) {
strMAC = str.substring(str.indexOf("MAC Address") + 14,
str.length());
break;
}
}
}
} catch (IOException ex) {
return "Can't Get MAC Address!";
}
//
if (strMAC.length() < 17) {
return "Error!";
}
macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)
+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)
+ ":" + strMAC.substring(12, 14) + ":"
+ strMAC.substring(15, 17);
//
return macAddress;
}
public static void main(String[] args) {
GetMACAddress getMACAddress = new GetMACAddress();
System.out.println(getMACAddress.getMACAddress("59.78.63.38")); //獲得該ip地址的mac地址
}
public static String procAll(String str) {
return procStringEnd(procFirstMac(procAddress(str)));
}
public static String procAddress(String str) {
int indexof = str.indexOf("Physical Address");
if (indexof > 0) {
return str.substring(indexof, str.length());
}
return str;
}
public static String procFirstMac(String str) {
int indexof = str.indexOf(":");
if (indexof > 0) {
return str.substring(indexof + 1, str.length()).trim();
}
return str;
}
public static String procStringEnd(String str) {
int indexof = str.indexOf("\r");
if (indexof > 0) {
return str.substring(0, indexof).trim();
}
return str;
}
}
只要寫一個servlet來調用這個類的getMACAddress(String netip)方法就可以獲得客戶端的mac地址了,相信你會寫jsp應該對servlet也不陌生吧,在jsp中調用servlet通過session傳遞返回的mac地址,加以判斷就可以了
mac地址是可以通過注冊表修改的,不建議以此來作為限制依據~
補充:
關于獲取IP地址的方式,最近在下有一個教訓,如果單純通過InetAddress來獲取IP地址,就會出現在不同的機器上IP地址不同的問題。
InetAddress.getLocalHost().getAddress() 實際上是根據hostname來獲取IP地址的。系統在剛剛裝完默認的hostname是localhost,所以通過上面代碼獲取到的本機 ip就是127.0.0.1, 相對應,比如我的hostname就是rjlin.atsig.com 返回的ip地址確是atsig.com的地址。暫時采用下面代碼來處理,當然還不夠靈活:
public static byte[] getIp() throws UnknownHostException {
byte[] b = InetAddress.getLocalHost().getAddress();
Enumeration allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
InetAddress ip = null;
NetworkInterface netInterface = null;
while (allNetInterfaces.hasMoreElements()) {
netInterface = (NetworkInterface) allNetInterfaces.nextElement();
if (netInterface.getName().trim().equals("eth0")){
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
}
break;
}
}
if (ip != null && ip instanceof Inet4Address) {
return b = ip.getAddress();
}
return b;
}
補充:
// 獲取真實IP的方法().
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載