Java获取客户端公网IP

查看本机的公网IP:https://ip.cn/

目前总结了两种获取客户端的公网IP方法。

方法一:通过第三方网站进行获取()

public static void main(String[] args){
    String ip = "";
    String chinaz = "http://ip.chinaz.com";
    
    StringBuilder inputLine = new StringBuilder();
    String read = "";
    URL url = null;
    HttpURLConnection urlConnection = null;
    BufferedReader in = null;
    try {
        url = new URL(chinaz);
        urlConnection = (HttpURLConnection) url.openConnection();
        in = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
        while((read=in.readLine())!=null){
            inputLine.append(read+"\r\n");
        }
        //System.out.println(inputLine.toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        if(in!=null){
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    
    Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
    Matcher m = p.matcher(inputLine.toString());
    if(m.find()){
        String ipstr = m.group(1);
        ip = ipstr;
        //System.out.println(ipstr);
    }
    System.out.println(ip);
}

第二种方法:通过http请求头部获取(别人测试ok,但本人测试只能获取到本机的内网IP)

package com.ehking.commons.web.utils;

import org.apache.commons.lang.StringUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

public class IpUtils {
    private IpUtils() {
    }

    public static String getIpAddr(HttpServletRequest request) {
        if (request == null) {
            return "unknown";
        }
        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("X-Forwarded-For");
        }
        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("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            try {
                ip = request.getRemoteAddr();
            }catch (Throwable e){
                e.printStackTrace();
            }
            if(StringUtils.isBlank(ip)||"127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)){  
                //根据网卡取本机配置的IP  
                InetAddress inet=null;  
                try {  
                    inet = InetAddress.getLocalHost();  
                } catch (UnknownHostException e) {  
                    e.printStackTrace();  
                }
                if(null != inet){
                    ip= inet.getHostAddress();
                }
            }  
        }
        if(StringUtils.isNotBlank(ip) && ip.contains(",")){
            ip=ip.split(",")[0];
        }
        return StringUtils.isNotBlank(ip)?ip.trim():null;
    }
}

  

posted @ 2020-02-28 22:17  R小哥  阅读(1291)  评论(0编辑  收藏  举报