• java 获取的是本地的IP地址


    1 public static void main(String[] args) {
     2       try {
     3              InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
     4              String hostAddress = address.getHostAddress());//192.168.0.121            
     5              InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");//获取的是该网站的ip地址,比如我们所有的请求都通过nginx的,所以这里获取到的其实是nginx服务器的IP地 
     6              String hostAddress1 = address1.getHostAddress());//124.237.121.122 
     7              InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");//根据主机名返回其可能的所有InetAddress对象 
     8              for(InetAddress addr:addresses){ 
     9              System.out.println(addr);//www.baidu.com/14.215.177.38 
    10              //www.baidu.com/14.215.177.37 
    11            } 
    12         } catch (UnknownHostException e) { 
    13              e.printStackTrace();
    14       } 
    15  }


    /**
    * 获取服务器IP地址
    * @return
    */
    @SuppressWarnings("unchecked")
    public static String getServerIp(){
    String SERVER_IP = null;
    try {
    Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
    InetAddress ip = null;
    while (netInterfaces.hasMoreElements()) {
    NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
    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 (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    return SERVER_IP;
    }
    }

  • 相关阅读:
    ABP拦截器之UnitOfWorkRegistrar(一)
    ABP中的拦截器之EntityHistoryInterceptor
    ABP中的拦截器之AuditingInterceptor
    ABP中的拦截器之ValidationInterceptor(下)
    ABP中的拦截器之ValidationInterceptor(上)
    ABP中模块初始化过程(二)
    ABP中的模块初始化过程(一)
    工欲善其事必先利其器
    ie9/8的iframe中jQuery报错
    git仓库删除所有提交历史记录
  • 原文地址:https://www.cnblogs.com/duanqiao123/p/8395115.html
Copyright © 2020-2023  润新知