Java Get Client Access IP

Source: Internet
Author: User

Original: http://blog.csdn.net/mydwr/article/details/9357187

/*** Get Visitor ip* * In general, use REQUEST.GETREMOTEADDR (), but after the reverse proxy software such as Nginx, this method will fail. * * This method first obtains the X-real-ip from the header, if it does not exist then obtains the first IP from the X-forwarded-for (with, splits), * if does not already exist then calls the request. GETREMOTEADDR (). * * @paramrequest*@return*/ Public StaticString getipaddr (HttpServletRequest request)throwsexception{string IP= Request.getheader ("X-real-ip");if(! Stringutils.isblank (IP) &&! " Unknown. Equalsignorecase (IP)) {returnIP;} IP= Request.getheader ("X-forwarded-for");if(! Stringutils.isblank (IP) &&! " Unknown. Equalsignorecase (IP)) {//multiple reverse proxies will have more than one IP value, the first being a real IP. intindex = Ip.indexof (', ');if(Index! =-1) {returnIp.substring (0, index);} Else {returnIP;}} Else {returnrequest.getremoteaddr ();}}

Java Get Client Access IP

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.