import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
public class ClientIpUtil {
/**
* 获取客户端IP地址
* @Title: getIpAddr
* @Description: 获取客户端IP地址
* @author: 王钧
* @date 2015-9-18 下午03:58:58
* @param request
* @return String
*/
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
//ipAddress = this.getRequest().getRemoteAddr();
ipAddress = request.getHeader("x-forwarded-for");
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if(ipAddress.equals("127.0.0.1")){
//根据网卡取本机配置的IP
InetAddress inet=null;
try {
inet = InetAddress.getLocalHost();
}catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress= inet.getHostAddress();
}
}
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
if(ipAddress.indexOf(",")>0){
ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
}
}
return ipAddress;
}
/**
* 获取本机MAC地址
* @Title: getLocalMACAddr
* @Description: 获取本机MAC地址
* @author: 王钧
* @date 2015-9-18 下午02:35:05
* @throws SocketException
* @throws UnknownHostException
*/
public static void getLocalMACAddr()throws SocketException, UnknownHostException {
// 获得IP
NetworkInterface netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
// 获得Mac地址的byte数组
byte[] macAddr = netInterface.getHardwareAddress();
System.out.print("MAC Addr:\t");
// 循环输出
for (byte b : macAddr) {
// 这里的toHexString()是自己写的格式化输出的方法
System.out.print(toHexString(b) + " ");
}
}
private static String toHexString(int integer) {
// 将得来的int类型数字转化为十六进制数
String str = Integer.toHexString((int) (integer & 0xff));
// 如果遇到单字符,前置0占位补满两格
if (str.length() == 1) {
str = "0" + str;
}
return str;
}
/**
* 根据来访客户端IP获取客户端MAC地址
* @Title: getMacAddr
* @Description: 根据来访客户端IP获取客户端MAC地址
* @author: 王钧
* @date 2015-9-18 下午02:37:42
* @param clientIp
* @return String
*/
public static String getMacAddr(String clientIp){
String macAddr = null;
try{
Process p = Runtime.getRuntime().exec("arp -a " + clientIp);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
p.waitFor();
boolean flag = true;
String str = null;
while(flag) {
str = input.readLine();
//System.out.println("11:"+str);
if (str != null) {
if (str.indexOf(clientIp) > 1) {
int temp = str.indexOf("at");
if (temp>0){//说明服务器是linux系统
macAddr = str.substring(temp + 3, temp + 20);
}else{//否则说明服务器是WINDOWS系统
temp = str.indexOf("-");
macAddr = str.substring(temp - 2, temp + 15);
}
break;
}
}else{
flag = false;
}
}
}catch (Exception e) {
e.printStackTrace(System.out);
}
return macAddr;
}
public static void main(String[] args) throws SocketException, UnknownHostException{
System.out.print(getMacAddr("192.168.0.34"));
}
}
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
public class ClientIpUtil {
/**
* 获取客户端IP地址
* @Title: getIpAddr
* @Description: 获取客户端IP地址
* @author: 王钧
* @date 2015-9-18 下午03:58:58
* @param request
* @return String
*/
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
//ipAddress = this.getRequest().getRemoteAddr();
ipAddress = request.getHeader("x-forwarded-for");
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if(ipAddress.equals("127.0.0.1")){
//根据网卡取本机配置的IP
InetAddress inet=null;
try {
inet = InetAddress.getLocalHost();
}catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress= inet.getHostAddress();
}
}
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
if(ipAddress.indexOf(",")>0){
ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
}
}
return ipAddress;
}
/**
* 获取本机MAC地址
* @Title: getLocalMACAddr
* @Description: 获取本机MAC地址
* @author: 王钧
* @date 2015-9-18 下午02:35:05
* @throws SocketException
* @throws UnknownHostException
*/
public static void getLocalMACAddr()throws SocketException, UnknownHostException {
// 获得IP
NetworkInterface netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
// 获得Mac地址的byte数组
byte[] macAddr = netInterface.getHardwareAddress();
System.out.print("MAC Addr:\t");
// 循环输出
for (byte b : macAddr) {
// 这里的toHexString()是自己写的格式化输出的方法
System.out.print(toHexString(b) + " ");
}
}
private static String toHexString(int integer) {
// 将得来的int类型数字转化为十六进制数
String str = Integer.toHexString((int) (integer & 0xff));
// 如果遇到单字符,前置0占位补满两格
if (str.length() == 1) {
str = "0" + str;
}
return str;
}
/**
* 根据来访客户端IP获取客户端MAC地址
* @Title: getMacAddr
* @Description: 根据来访客户端IP获取客户端MAC地址
* @author: 王钧
* @date 2015-9-18 下午02:37:42
* @param clientIp
* @return String
*/
public static String getMacAddr(String clientIp){
String macAddr = null;
try{
Process p = Runtime.getRuntime().exec("arp -a " + clientIp);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
p.waitFor();
boolean flag = true;
String str = null;
while(flag) {
str = input.readLine();
//System.out.println("11:"+str);
if (str != null) {
if (str.indexOf(clientIp) > 1) {
int temp = str.indexOf("at");
if (temp>0){//说明服务器是linux系统
macAddr = str.substring(temp + 3, temp + 20);
}else{//否则说明服务器是WINDOWS系统
temp = str.indexOf("-");
macAddr = str.substring(temp - 2, temp + 15);
}
break;
}
}else{
flag = false;
}
}
}catch (Exception e) {
e.printStackTrace(System.out);
}
return macAddr;
}
public static void main(String[] args) throws SocketException, UnknownHostException{
System.out.print(getMacAddr("192.168.0.34"));
}
}