温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么获取当前服务器或用户ip和端口

发布时间:2021-07-07 16:29:05 来源:亿速云 阅读:880 作者:chen 栏目:大数据

这篇文章主要介绍“怎么获取当前服务器或用户ip和端口”,在日常操作中,相信很多人在怎么获取当前服务器或用户ip和端口问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么获取当前服务器或用户ip和端口”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

 经过请求(request)获取当前服务器地址 相信基本都知道

request.getHeader("User-Agent");    //就是取得客户端的系统版本     
request.getRemoteAddr();    //取得客户端的IP     
request.getRemoteHost()     //取得客户端的主机名     
request.getRemotePort();    //取得客户端的端口     
request.getRemoteUser();    //取得客户端的用户     
request.getLocalAddr();    //取得服务器IP     
request.getLocalPort();    //取得服务器端口

   但是不经过请求获取服务器地址 经测试 以下两个方法都可以

//方法1
  String SERVER_IP ="";
        try {
            Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) netInterfaces
                        .nextElement();
                ni.getInterfaceAddresses();
                ip = (InetAddress) ni.getInetAddresses().nextElement();
                SERVER_IP = ip.getHostAddress();
                if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
                        && ip.getHostAddress().indexOf(":") == -1) {
                    SERVER_IP = ip.getHostAddress();
                    break;
                } else {
                    ip = null;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 //方法2
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        }catch (Exception e) {
            e.printStackTrace();
        }
        byte[] ipAddr = addr.getAddress();
        String ipAddrStr = "";
        for (int i = 0; i < ipAddr.length; i++) {
            if (i > 0) {
                ipAddrStr += ".";
            }
            ipAddrStr += ipAddr[i] & 0xFF;
        }
         System.out.println(ipAddrStr);

    如果同一台服务器下 部署多个应用 (多端口)  一直没有找到 如何判断端口

    例如:如果多service   

   个人使用方法 获取项目根目录地址判断 service  部署地址

//取得根目录路径
 String rootPath=getClass().getResource("/").getFile().toString();

  如果使用反向代理 获取用户ip

   public static String getRemoteIpAddress(){
      HttpServletRequest request = getHttpServletRequest();
      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();
        }
        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.indexOf(",") != -1) { 
       ip = ip.substring(ip.lastIndexOf(",") + 1, ip.length()).trim();  
      }
        if("0:0:0:0:0:0:0:1".equals(ip))
        {
          ip="127.0.0.1";
        }
        return ip;
    }

到此,关于“怎么获取当前服务器或用户ip和端口”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI