PackageCom.javaweb.util;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.LineNumberReader;Importjavax.servlet.http.HttpServletRequest; Public classclientinformation {//Get the client IP address Public Staticstring 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 ();}returnIP;}//get the client MAC address Public Staticstring getmacaddress (String IP) {string str= ""; String macAddress= ""; System.out.println ("IPPPPPPPPPPPPPPPPPP" +IP);Try{Process P= Runtime.getruntime (). EXEC ("cmd/c c:\\windows\\sysnative\\nbtstat.exe-a" +IP); inputstreamreader ir=NewInputStreamReader (P.getinputstream ()); LineNumberReader input=NewLineNumberReader (IR); for(inti = 1; I < 100; i++) {str=input.readline ();if(str! =NULL) {if(Str.indexof ("MAC") > 1) {macAddress= Str.substring (Str.indexof ("=") + 2, Str.length ()); Break;}}}} Catch(IOException e) {e.printstacktrace (System.out);}returnmacAddress;}//get the client computer name Public Staticstring getcomputername (String IP) {string ComputerName= ""; String Str= "";Try{Process P= Runtime.getruntime (). EXEC ("cmd/c c:\\windows\\sysnative\\nbtstat.exe-a" +IP); inputstreamreader ir=NewInputStreamReader (P.getinputstream ()); LineNumberReader input=NewLineNumberReader (IR); for(inti = 1; I < 100; i++) {Try{str=input.readline ();} Catch(IOException e) {//TODO auto-generated Catch blocke.printstacktrace ();}if(Str.indexof ("unique") > 1) {ComputerName= str.substring (0, Str.indexof ("<") . Trim (); Break;}}} Catch(IOException e) {//TODO auto-generated Catch blocke.printstacktrace ();}returncomputerName;}}
Java in Win7 64-bit Get client Ip,mac, computer name