用Java获取本机IP或者请求用户的真正IP地址

一、在Web请求中获取请求用户的IP地址

publicstaticString getUserRealIP(HttpServletRequest request)throws UnknownHostException {

        String ip = "";

        // 有的user可能使用代理,为处理用户使用代理的情况,使用x-forwarded-forif(request.getHeader("x-forwarded-for") ==null)  {

        ip = request.getRemoteAddr();

        }  else  {

        ip = request.getHeader("x-forwarded-for");

        }

        if("127.0.0.1".equals(ip))  {

        // 获取本机真正的ip地址        ip = InetAddress.getLocalHost().getHostAddress();

        }

        return ip;

        }

二、获取本机IP地址和主机名

importjava.util.*;

        importjava.net.*;publicclass getMyIP

{

    publicstaticvoid main(String[] args) {

        String ip =null;

        String host =null;

        try{

            InetAddress ia = InetAddress.getLocalHost();

            host = ia.getHostName();//获取计算机名字            ip = ia.getHostAddress();//获取IP        } catch(UnknownHostException e) {

            e.printStackTrace();

        }

        System.out.println(host);

        System.out.println(IP);

    }

}

三、网上搜索的方法

import javax.servlet.http.HttpServletRequest;/** * 自定义访问对象工具类

*

* 获取对象的IP地址等信息

* @author X-rapido

*

*/publicclass CusAccessObjectUtil {

    /**    * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,

    *

    * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢?

    * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。

    *

    * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130,

    * 192.168.1.100

    *

    * 用户真实IP为: 192.168.1.110

    *

    * @param request

    * @return*/publicstatic String getIpAddress(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();

            if("127.0.0.1".equals(ip)||"0:0:0:0:0:0:0:1".equals(ip)){

                //根据网卡取本机配置的IPInetAddress inet=null;

                try {

                    inet = InetAddress.getLocalHost();

                } catch (UnknownHostException e) {

                    e.printStackTrace();

                }

                ip= inet.getHostAddress();

            }

        }

        return ip;

    }

}

四、扩展知识

Java获取客户端请求电脑IP,操作系统等等各种信息

HttpServletRequest request = ServletActionContext.getRequest();

        System.out.println("浏览器基本信息:"+request.getHeader("user-agent"));

        System.out.println("客户端系统名称:"+System.getProperty("os.name"));

        System.out.println("客户端系统版本:"+System.getProperty("os.version"));

        System.out.println("客户端操作系统位数:"+System.getProperty("os.arch"));

        System.out.println("HTTP协议版本:"+request.getProtocol());

        System.out.println("请求编码格式:"+request.getCharacterEncoding());

        System.out.println("Accept:"+request.getHeader("Accept"));

        System.out.println("Accept-语言:"+request.getHeader("Accept-Language"));

        System.out.println("Accept-编码:"+request.getHeader("Accept-Encoding"));

        System.out.println("Connection:"+request.getHeader("Connection"));

        System.out.println("Cookie:"+request.getHeader("Cookie"));

        System.out.println("客户端发出请求时的完整URL"+request.getRequestURL());

        System.out.println("请求行中的资源名部分"+request.getRequestURI());

        System.out.println("请求行中的参数部分"+request.getRemoteAddr());

        System.out.println("客户机所使用的网络端口号"+request.getRemotePort());

        System.out.println("WEB服务器的IP地址"+request.getLocalAddr());

        System.out.println("WEB服务器的主机名"+request.getLocalName());

        System.out.println("客户机请求方式"+request.getMethod());

        System.out.println("请求的文件的路径"+request.getServerName());

        System.out.println("请求体的数据流"+request.getReader());

        BufferedReader br=request.getReader();

        String res = "";

        while((res = br.readLine()) !=null) {

        System.out.println("request body:" + res);

        }

        System.out.println("请求所使用的协议名称"+request.getProtocol());

        System.out.println("请求中所有参数的名字"+request.getParameterNames());

        Enumeration enumNames= request.getParameterNames();

        while (enumNames.hasMoreElements()) {

        String key = (String) enumNames.nextElement();

        System.out.println("参数名称:"+key);

        }

五、在本机上获取计算机信息

publicvoid getIP(){

        try {

        InetAddress address = InetAddress.getLocalHost();

        String ip = address.getHostAddress().toString();//获取本机IPString hostName = address.getHostName().toString();//获取本机计算机名称System.out.println("ip is:"+ip);

        System.out.println("host name is:"+hostName);

        } catch (UnknownHostException e) {

        e.printStackTrace();

        }

        }

个人博客

你可能感兴趣的:(用Java获取本机IP或者请求用户的真正IP地址)