Java 如何获取客户端IP呢?

/**
     * 获取客户端ip地址(可以穿透代理)
     * 
     * @param request
     * @return
     */
    public static String getRemoteAddr(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 ip;
    }
    private static final String[] HEADERS_TO_TRY = { 
        "X-Forwarded-For",
        "Proxy-Client-IP",
        "WL-Proxy-Client-IP",
        "HTTP_X_FORWARDED_FOR",
        "HTTP_X_FORWARDED",
        "HTTP_X_CLUSTER_CLIENT_IP",
        "HTTP_CLIENT_IP",
        "HTTP_FORWARDED_FOR",
        "HTTP_FORWARDED",
        "HTTP_VIA",
        "REMOTE_ADDR",
        "X-Real-IP"};

    /***
     * 获取客户端ip地址(可以穿透代理)
     * @param request
     * @return
     */
    public static String getClientIpAddress(HttpServletRequest request) {
        for (String header : HEADERS_TO_TRY) {
            String ip = request.getHeader(header);
            if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
                return ip;
            }
        }
        return request.getRemoteAddr();
    }
    /***
     * 获取客户端ip地址(可以穿透代理)
     * @param request
     * @return
     */
    public static String getClientIpAddr(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_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_CLUSTER_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_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_FORWARDED");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_VIA");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("REMOTE_ADDR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    }
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (null != ip && !"".equals(ip.trim())
                && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
        ip = request.getHeader("X-Forwarded-For");
        if (null != ip && !"".equals(ip.trim())
                && !"unknown".equalsIgnoreCase(ip)) {
            // get first ip from proxy ip
            int index = ip.indexOf(',');
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        return request.getRemoteAddr();
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114

我使用的是spring MVC框架,测试的控制器代码如下:

package com.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.SystemHWUtil;
import com.common.util.WebServletUtil;
import com.string.widget.util.ValueWidget;
import com.util.JSONPUtil;
/***
 * 用于测试跨域
 * @author huangweii
 * 2015年5月29日
 */
@Controller
@RequestMapping("/cors")
public class CORSController {
    @ResponseBody
    @RequestMapping(value = "/simple",produces=SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)
    public String corsJsonSimple(HttpServletRequest request,String callback){
        String content;
        Map map=new HashMap();
        map.put("username", "黄威");
        map.put("age", "27");
        map.put("address", "beijing");
        content=JSONPUtil.getJsonP(map, callback);
        System.out.println("getIpAddr:"+WebServletUtil.getIpAddr(request));
        System.out.println("getRemoteAddr:"+WebServletUtil.getRemoteAddr(request));
        System.out.println("getClientIpAddr:"+WebServletUtil.getClientIpAddr(request));
        System.out.println("getClientIpAddress:"+WebServletUtil.getClientIpAddress(request));
        return content;
    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

测试结果: 
获取客户端IP

获取登录客户端ip和mac

  1.  /** 
  2.  * 通过HttpServletRequest返回IP地址 
  3.  * @param request HttpServletRequest 
  4.  * @return ip String 
  5.  * @throws Exception 
  6.  */  
  7. public String getIpAddr(HttpServletRequest request) throws Exception {  
  8.     String ip = request.getHeader("x-forwarded-for");  
  9.     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  10.         ip = request.getHeader("Proxy-Client-IP");  
  11.     }  
  12.     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  13.         ip = request.getHeader("WL-Proxy-Client-IP");  
  14.     }  
  15.     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  16.         ip = request.getHeader("HTTP_CLIENT_IP");  
  17.     }  
  18.     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  19.         ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
  20.     }  
  21.     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  22.         ip = request.getRemoteAddr();  
  23.     }  
  24.     return ip;  
  25. }  


Java代码   收藏代码
  1.  /** 
  2.  * 通过IP地址获取MAC地址 
  3.  * @param ip String,127.0.0.1格式 
  4.  * @return mac String 
  5.  * @throws Exception 
  6.  */  
  7. public String getMACAddress(String ip) throws Exception {  
  8.     String line = "";  
  9.     String macAddress = "";  
  10.     final String MAC_ADDRESS_PREFIX = "MAC Address = ";  
  11.     final String LOOPBACK_ADDRESS = "127.0.0.1";  
  12.     //如果为127.0.0.1,则获取本地MAC地址。  
  13.     if (LOOPBACK_ADDRESS.equals(ip)) {  
  14.         InetAddress inetAddress = InetAddress.getLocalHost();  
  15.         //貌似此方法需要JDK1.6。  
  16.         byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();  
  17.         //下面代码是把mac地址拼装成String  
  18.         StringBuilder sb = new StringBuilder();  
  19.         for (int i = 0; i < mac.length; i++) {  
  20.             if (i != 0) {  
  21.                 sb.append("-");  
  22.             }  
  23.             //mac[i] & 0xFF 是为了把byte转化为正整数  
  24.             String s = Integer.toHexString(mac[i] & 0xFF);  
  25.             sb.append(s.length() == 1 ? 0 + s : s);  
  26.         }  
  27.         //把字符串所有小写字母改为大写成为正规的mac地址并返回  
  28.         macAddress = sb.toString().trim().toUpperCase();  
  29.         return macAddress;  
  30.     }  
  31.     //获取非本地IP的MAC地址  
  32.     try {  
  33.         Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);  
  34.         InputStreamReader isr = new InputStreamReader(p.getInputStream());  
  35.         BufferedReader br = new BufferedReader(isr);  
  36.         while ((line = br.readLine()) != null) {  
  37.             if (line != null) {  
  38.                 int index = line.indexOf(MAC_ADDRESS_PREFIX);  
  39.                 if (index != -1) {  
  40.                     macAddress = line.substring(index + MAC_ADDRESS_PREFIX.length()).trim().toUpperCase();  
  41.                 }  
  42.             }  
  43.         }  
  44.         br.close();  
  45.     } catch (IOException e) {  
  46.         e.printStackTrace(System.out);  
  47.     }  
  48.     return macAddress;  
  49. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值