zoukankan      html  css  js  c++  java
  • 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+"
    ");
            }
            //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;
        }
    }
    

      

    愿我们漂泊半生, 归来仍少年!
  • 相关阅读:
    Mysql 8.0 OCP认证考试原题题库整理(CUUG内部资料)-第2题
    Mysql 8.0 OCP认证考试原题题库整理(CUUG内部资料)-第1题
    【2020年8月】Oracle OCP 062考试新题(-3题)CUUG内部题库
    【2020年8月】Oracle OCP 062考试新题(-2题)CUUG内部题库
    iOS7时代我们用什么来追踪和识别用户?
    插件就是生产力——那些不能错过的XCode插件们
    批处理执行Testng
    Jmeter调度器小记
    Newtonsoft.Json小记
    批处理添加环境变量
  • 原文地址:https://www.cnblogs.com/Lonnn/p/12380653.html
Copyright © 2011-2022 走看看