Note: There are two limitations to the code:
1. The client must be a Windows operating system
2. The program code deployed to Linux when the desired effect, access to the IP is 127.0.0.1 so IP access to the MAC address is also inaccurate, to be improved ....
3. Please the enthusiastic friend message comments, thanks for pointing the maze!
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.LineNumberReader;
Import Javax.servlet.http.HttpServletRequest;
public class Getipmacbywindows {//Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
/** * Use Nginx reverse proxy, get client real IP address. * @return Client IP address */public static string GetIP (HttpServletRequest request) {String IP = request.getheader ("X_forwa
Rded_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 ();
} if (Ip.length () >) {string[] ips = Ip.split (","); for (int index = 0; index < ips.length; index++) {String strIp = (String) ips[indEX]; if (! ()
Unknown ". Equalsignorecase (Strip)) {IP = strip;
Break
}} return IP;
/** * Gets the MAC address of the client. * @return MAC address/public static string Getmacbywindows (HttpServletRequest request) {//Get the true IP String for this client realip
= GetIP (request);
Obtain client MAC address String mac_s = getmacaddress (realip) via IP;
return mac_s;
//IP Get client MAC address public static string getmacaddress (string IP) {string macaddr = "";
try {Process p = runtime.getruntime (). EXEC ("nbtstat-a" + IP);
InputStreamReader ir = new InputStreamReader (P.getinputstream (), "GBK");
LineNumberReader input = new LineNumberReader (IR);
for (int i = 1; i < i++) {String str = input.readline ();//System.out.println (str); Gets the client MAC address of line 14th if (i==14){macaddr = str.split ("=") [1].trim ();
A catch (IOException e) {e.printstacktrace (System.out);
return macaddr; }
}
3.nginx Configuration