Java 获取客户端请求IP地址(公网ip)

Posted 雁书几封

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 获取客户端请求IP地址(公网ip)相关的知识,希望对你有一定的参考价值。

 

 

package sdcncsi.ict.util.redis;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description
 * @Author
 * @Date
 */
public class IPUtil {

    /**
     *
     * @Description:获取客户端的IP
     * @author
     * @date 2020/05/07 10:51
     */
    public static 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.getRemoteAddr();
        }
        return ip;
    }

    /**
     * @Description:获取客户端外网ip 此方法要接入互联网才行,内网不行
     * @Author:
     * @Date:2020/05/07 10:51
     **/
    public static String getPublicIp() {
        try {
            String path = "http://www.net.cn/static/customercare/yourip.asp";// 要获得html页面内容的地址

            URL url = new URL(path);// 创建url对象

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 打开连接

            conn.setRequestProperty("contentType", "GBK"); // 设置url中文参数编码

            conn.setConnectTimeout(5 * 1000);// 请求的时间

            conn.setRequestMethod("GET");// 请求方式

            InputStream inStream = conn.getInputStream();
            // readLesoSysXML(inStream);

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    inStream, "GBK"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            // 读取获取到内容的最后一行,写入
            while ((line = in.readLine()) != null) {
                buffer.append(line);
            }
            List<String> ips = new ArrayList<String>();

            //用正则表达式提取String字符串中的IP地址
            String regEx="((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)";
            String str = buffer.toString();
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(str);
            while (m.find()) {
                String result = m.group();
                ips.add(result);
            }
            String PublicIp = ips.get(0);

            // 返回公网IP值
            return PublicIp;

        } catch (Exception e) {
            System.out.println("获取公网IP连接超时");
            return "";
        }
    }

}

 

以上是关于Java 获取客户端请求IP地址(公网ip)的主要内容,如果未能解决你的问题,请参考以下文章

php获取客户端公网ip代码

JAVA获取公网ip

Java根据HttpServletRequest请求获取服务器的IP地址

请问如何用java获取公网IP?

java 获取web登录者的ip地址

java 获取客户端ip地址