获取ip地址

/** (第一种方法)
* 这是最简单的方法,但也容易出问题,获取的IP地址可能是:127.0.0.1 或其他,而并不是客户端的真实IP。
*/
public static void main(String[] args)    {
        try{
            // 获取计算机名
            String name = InetAddress.getLocalHost().getHostName();
            // 获取IP地址
            String ip = InetAddress.getLocalHost().getHostAddress();
            System.out.println("计算机名:"+name);
            System.out.println("IP地址:"+ip);
        }
        catch (UnknownHostException e){
            System.out.println("异常:" + e);
            e.printStackTrace();
        }
    }
    /** (第二种方法)
     * 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址;
     *
     * @param request
     * @return
     */
    public final static String getIpAddress(HttpServletRequest request) {
        // 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
        String ip = request.getHeader("X-Forwarded-For");
        try {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                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();
                }
            } else if (ip.length() > 15) {
                String[] ips = ip.split(",");
                for (int index = 0; index < ips.length; index++) {
                    String strIp = (String) ips[index];
                    if (!("unknown".equalsIgnoreCase(strIp))) {
                        ip = strIp;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ip;
    }

注意:第二种方法获取的地址可能是0:0:0:0:0:0:0:1  在URL上的不用localhost即可
/** (第三种方法) 
* 直接通过运行cmd获取ip地址 
*
* @return 
*/
public static String getLocalIPForCMD() {
    StringBuilder sb = new StringBuilder();
    String command = "cmd.exe /c ipconfig | findstr IPv4";
    try {
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        while ((line = br.readLine()) != null) {
            line = line.substring(line.lastIndexOf(":") + 2, line.length());
            sb = new StringBuilder();
            sb.append(line);
        }
        br.close();
        p.destroy();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值