Java Get real IP address

Source: Internet
Author: User

When the project is released into the network, the host (such as IP address 192.168.10.88) accesses the URL of the LAN server, such as http://192.168.10.142:8080/index/, with request.getremoteaddr ( the host IP address obtained is 192.168.10.142, not the real IP address 192.168.10.88. To solve this problem, the following methods can be used to avoid this problem.

Public static string getipaddress (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 ()  ==&NBsp;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.equals ("0:0:0:0:0:0:0:1")  ?  " 127.0.0.1 " : IP;    }

When the server and the host are the same machine, the obtained address is 0:0:0:0:0:0:0:1,0:0:0:0:0:0:0:1 is the representation of IPv6, corresponding to IPv4 equivalent to 127.0.0.1. Many of the arguments are that the #::1 localhost (IPv6) in the Hosts file (C:\Windows\System32\drivers\etc folder) is commented out and can get to 127.0.0.1, but I tried it without effect. So we used a three-mesh operator to assign a value of 127.0.01.


This article is from the "hualiued" blog, make sure to keep this source http://hualiued.blog.51cto.com/11510677/1887295

Java Get real IP address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.