需要對用戶的 ip 和 mac 地址進行驗證
jsp頁面下
因為系統的VIew層是用JSF來實現的
Java代碼
public String getMyIP() {
try {
FacesContext fc = FacesContext
HttpServletRequest request = (HttpServletRequest)fc
return request
}
catch (Exception e) {
e
}
return
}
調用window的命令
Java代碼
public String getMACAddress(String ip){
String str =
String macAddress =
try {
Process p = Runtime
InputStreamReader ir = new InputStreamReader(p
LineNumberReader input = new LineNumberReader(ir);
for (int i =
str = input
if (str != null) {
if (str
macAddress = str
break;
}
}
}
} catch (IOException e) {
e
}
return macAddress;
}
完整代碼
Java代碼
import java
import java
import java
public class GetMACAddress {
public String getMACAddress(String ipAddress) {
String str =
try {
Process pp = Runtime
InputStreamReader ir = new InputStreamReader(pp
LineNumberReader input = new LineNumberReader(ir);
for (int i =
str = input
if (str != null) {
if (str
strMAC = str
str
break;
}
}
}
} catch (IOException ex) {
return
}
//
if (strMAC
return
}
macAddress = strMAC
+
+
+ strMAC
//
return macAddress;
}
public static void main(String[] args) {
GetMACAddress getMACAddress = new GetMACAddress();
System
}
public static String procAll(String str) {
return procStringEnd(procFirstMac(procAddress(str)));
}
public static String procAddress(String str) {
int indexof = str
if (indexof >
return str
}
return str;
}
public static String procFirstMac(String str) {
int indexof = str
if (indexof >
return str
}
return str;
}
public static String procStringEnd(String str) {
int indexof = str
if (indexof >
return str
}
return str;
}
}
只要寫一個servlet來調用這個類的getMACAddress(String netip)方法就可以獲得客戶端的mac地址了
mac地址是可以通過注冊表修改的
補充
關於獲取IP地址的方式
InetAddress
public static byte[] getIp() throws UnknownHostException {
byte[] b = InetAddress
Enumeration allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface
} catch (SocketException e) {
e
}
InetAddress ip = null;
NetworkInterface netInterface = null;
while (allNetInterfaces
netInterface = (NetworkInterface) allNetInterfaces
if (netInterface
Enumeration addresses = netInterface
while (addresses
ip = (InetAddress) addresses
}
break;
}
}
if (ip != null && ip instanceof Inet
return b = ip
}
return b;
}
補充
// 獲取真實IP的方法()
public String getIpAddr(HttpServletRequest request) {
String ip = request
if(ip == null || ip
ip = request
}
if(ip == null || ip
ip = request
}
if(ip == null || ip
ip = request
}
return ip;
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26784.html