Java Get IP Address

Source: Internet
Author: User
Tags get ip

Tag:email   case    Reverse proxy    stat   header    remote   request   empty   alibaba   

Package Utils;import com.alibaba.druid.util.stringutils;import javax.servlet.http.httpservletrequest;/** * IP Address * * @ Author Zengwei * @email [email protected] * @date 2018-03-23 22:02 */public class Iputils {/** * Get IP Address * * Using the reverse proxy software such as Nginx, you cannot get IP address through REQUEST.GETREMOTEADDR () * If you use a multilevel reverse proxy, the value of x-forwarded-for is more than one, but a string of IP addresses, * The first non-unknown valid IP string in x-forwarded-for is the real IP address */public static string getipaddr (HttpServletRequest request) {string    IP = null;      try {ip = request.getheader ("X-forwarded-for"); if (Stringutils.isempty (IP) | |      "Unknown". Equalsignorecase (IP)) {IP = request.getheader ("Proxy-client-ip"); } if (Stringutils.isempty (IP) | | | ip.length () = = 0 | |      "Unknown". Equalsignorecase (IP)) {IP = request.getheader ("Wl-proxy-client-ip"); } if (Stringutils.isempty (IP) | |      "Unknown". Equalsignorecase (IP)) {IP = request.getheader ("Http_client_ip"); } if (Stringutils.isempty (IP) | | "Unknown". EqualsignorecasE (IP)) {IP = request.getheader ("Http_x_forwarded_for"); } if (Stringutils.isempty (IP) | |      "Unknown". Equalsignorecase (IP)) {IP = request.getremoteaddr ();  }} catch (Exception e) {} return IP;   }}

  

Java Get 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.