jfinal 使用者登入IP地址獲得[後端處理]
* ClassName: IpUtil
*
* @Description: 獲取客戶端使用者登入IP
*/
public class IpUtil {
/**
* 通過HttpServletRequest返回IP地址
*
* @param request
* HttpServletRequest
* @return ip String
* @throws Exception
*/
public static 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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
* 通過IP地址獲取MAC地址
*
* @param ip
* String,127.0.0.1格式
* @return mac String
* @throws Exception
*/
public static String getMACAddress(String ip) throws Exception {
String line = "";
String macAddress = "";
final String MAC_ADDRESS_PREFIX = "MAC Address = ";
final String LOOPBACK_ADDRESS = "127.0.0.1";
// 如果為127.0.0.1,則獲取本地MAC地址。
if (LOOPBACK_ADDRESS.equals(ip)) {
InetAddress inetAddress = InetAddress.getLocalHost();
// 貌似此方法需要JDK1.6。
byte[] mac = NetworkInterface.getByInetAddress(inetAddress)
.getHardwareAddress();
// 下面程式碼是把mac地址拼裝成String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// mac[i] & 0xFF 是為了把byte轉化為正整數
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
// 把字串所有小寫字母改為大寫成為正規的mac地址並返回
macAddress = sb.toString().trim().toUpperCase();
return macAddress;
}
// 獲取非本地IP的MAC地址
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (line != null) {
int index = line.indexOf(MAC_ADDRESS_PREFIX);
if (index != -1) {
macAddress = line
.substring(index + MAC_ADDRESS_PREFIX.length())
.trim().toUpperCase();
}
}
}
br.close();
} catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}
簡單封裝一個util類,在jfinal controller層中,用getRequest()方法獲得HttpServletRequest request物件,然後,呼叫方法
即可獲得使用者登入地址,如果沒有安裝防火牆,也可利用下面的方法得到實體地址(網絡卡地址)
public void index() throws Exception{
/*ServletRequestAttributes attr = (ServletRequestAttributes)
RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = attr.getRequest();*/
System.out.println("******************************");
String remoteHost = getRequest().getRemoteHost();
System.out.println("remoteHost:"+remoteHost);
System.out.println("******************************");
String remoteAddr = getRequest().getRemoteAddr();
System.out.println("remoteAddr:"+remoteAddr);
System.out.println("******************************");
HttpServletRequest request= getRequest();
String ipAddr = JspUtil.getIpAddr(request);
System.out.println("ipAddr:"+ipAddr+"***********dfsdfsf***************");
String macAddress = IpUtil.getMACAddress(ipAddr);
System.out.println("macAddress:"+ipAddr+"***********************");
System.out.println("macAddress:"+macAddress);