To get the IP of other users, you may encounter request.getremoteaddr () to get a value of 0:0:0:0:0:0:0:1, which is why, according to the truth, Should be 127.0.0.1, why the value of this to become a IPv6, and I found that this situation only in the server and the client are on the same computer will appear (for example, when the use of localhost access), and later on the Internet check the reason, the original is/etc/ Hosts this thing mischief (on Windows should be C:\Windows\system32\drivers\etc\ hosts this file), only need to comment out the file #:: 1 localhost This line can solve the problem. In addition, localhost this file is very useful, here you can add your own entries, such as adding 192.168.0.212 myweb this way, in the browser can only use 192.168.0.212来 access, and can use MyWeb to replace.
1 Publicloginnote Loginnoteip (String username, httpservletrequest request) {2 //Get IP3 //inetaddress netaddress = getinetaddress ();4 //your own definition of PO class5Loginnote Loginnote =Newloginnote ();6 //Get IP Address7String IP =request.getlocaladdr ();8 if(IP = =NULL|| Ip.length () = = 0) {9ip = Request.getheader ("X-forwarded-for");Ten if(IP = =NULL|| Ip.length () = = 0 One|| "Unknown". Equalsignorecase (IP)) { Aip = Request.getheader ("Proxy-client-ip"); - } - if(IP = =NULL|| Ip.length () = = 0 the|| "Unknown". Equalsignorecase (IP)) { -ip = Request.getheader ("Wl-proxy-client-ip"); - } - if(IP = =NULL|| Ip.length () = = 0 +|| "Unknown". Equalsignorecase (IP)) { -IP =request.getremoteaddr (); + } A } at //since some of the logins are through proxies, take the first one (the first one is really IP) - intindex = Ip.indexof (', '); - if(Index! =-1) { -ip = ip.substring (0, index); - } - loginnote.setip (IP); inLoginnote.settime (NewDate ()); - Loginnote.setusername (username); to returnLoginnote; +}View Code
If you only need to get your own IP, everyone login displays the IP of the server.
1 2InetAddress netaddress =getinetaddress ();3 //call the following code4 5 6 PrivateInetAddress getinetaddress () {Try{return7Inetaddress.getlocalhost (); }Catch(unknownhostexception e) {8SYSTEM.OUT.PRINTLN ("IP address not known"); }return NULL; }9 Ten Private StaticString Gethostip (inetaddress netaddress) {if(NULL== Onenetaddress) {return NULL; } String IP = netaddress.gethostaddress ();// A* Get IPreturnip }View Code
How Java obtains the true IP address of other users ' logins