首页 资讯 社群 我的社区 搜索

获取WEB 来访者的IP 地址,并根据来源IP 范围作出处理

木须虎
2019-03-09 20:38:25

前几天协助研发处理一个需求,WEB 站点可以根据来访者IP ,判断IP 所在的地址范围,自动切换不同的语言和TITLE 及LOGO 

其实这个需求并不复杂,获取来访者IP  ,直接从 request.getRemoteAddr() 就可以取得。

不知道为什么 java 的SOCKET  库没有提供 inet_addr()   Winsock2.h (windows)  &  arpa/inet.h (Linux)  都有的一个方法,可以把IP 转成一个无符号长整数。 这样我们通过整数的起止就不难计算出IP 的起止了。

inet_addr() 的原理并不复杂,IP 地址分为四段,每段是一个8位二进制数,我们自己计算一下就有了结果,不难用 JAVA 自己实现 inet_addr()  IP 地址转整数

 

package Servlet.GC;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "GCRemoteIP")
public class GCRemoteIP extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("GCRemoteIP.doGet" + getIpAddr(request));

        response.getWriter().println("doGet GCRemoteIP: " + getIpAddr(request)  + " Socket-Address: " + ip2Long(  getIpAddr(request)  ) );
    }

    private String 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();
         }
         **/
        return request.getRemoteAddr();
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    private  String long2IP(  long ip  ) {

        StringBuffer result = new StringBuffer();
        result .append(String.valueOf((ip >> 24)));
        result .append(".");
        result .append(String.valueOf((ip & 0x00FFFFFF) >> 16));
        result .append(".");
        result .append(String.valueOf((ip & 0x0000FFFF) >> 8));
        result .append(".");
        result .append(String.valueOf((ip & 0x000000FF)));
        System.out.println(result.toString());

        return result.toString();
    }

    private  Long ip2Long( String ip ) {

        String[] items = ip.split(".");
        long[] nums = new long[4];
        for (int i = 0; i < items.length; i++) {
            nums[i] = Long.parseLong(items[i]);
            //System.out.println(nums[i] + ":" + Long.toBinaryString(nums[i]));
        }
        long result = (nums[0] << 24) + (nums[1] << 16) + (nums[2] << 8) + nums[3];
        System.out.println(Long.toBinaryString(result));
        System.out.println(result);

        return result;
    }



}
用户评论