开发者社区> 问答> 正文

公网部署的项目 java后台取得服务器ip是内网的是怎么回事

公网部署的项目 java后台取得服务器ip是内网的ip是怎么回事

展开
收起
ysth 2019-01-05 14:58:08 5467 0
5 条回答
写回答
取消 提交回答
  • java也是通过底层操作系统的api来获取的,操作系统会返回所有的网卡界面,也会给你每个网卡界面的信息,你需要过滤一下内网IP即可。

    2019-07-17 23:24:39
    赞同 展开评论 打赏
  • 擅长互联网移动开发。。。

    需要中间件打通外网通道。

    2019-07-17 23:24:39
    赞同 展开评论 打赏
  • 先确定web服务器之前有没有代理层,如果没有的话参照一楼提供的方法试试;如果有代理层的话需要由上层代理把client端的ip传进来,一般是放header里,比如x-forwarded-for、Proxy-Client-IP等会将层层代理的ip加进来,然后在应用里去解析

    2019-07-17 23:24:39
    赞同 展开评论 打赏
  • 除了上面说的多个网络接口外,还有可能是你的服务端根本就在内网里部署。
    前置的接入层(nginx、apache)等是接入外网的,然后将流量打到内网服务器上。

    2019-07-17 23:24:39
    赞同 展开评论 打赏
  • 换api:NetworkInterface.getNetworkInterfaces() ,再做过滤,代码如下:

    转载:Java获得系统的外网IP
    https://www.cnblogs.com/kagome2014/p/6428325.html

    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.util.Enumeration;

    /**

    • 系统工具类,用于获取系统相关信息
    • Created by kagome.
      */

    public class CustomSystemUtil {

    public static String INTRANET_IP = getIntranetIp(); // 内网IP
    public static String INTERNET_IP = getInternetIp(); // 外网IP
    
    private CustomSystemUtil(){}
    
    /**
     * 获得内网IP
     * @return 内网IP
     */
    private static String getIntranetIp(){
        try{
            return InetAddress.getLocalHost().getHostAddress();
        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }
    
    /**
     * 获得外网IP
     * @return 外网IP
     */
    private static String getInternetIp(){
        try{
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            Enumeration<InetAddress> addrs;
            while (networks.hasMoreElements())
            {
                addrs = networks.nextElement().getInetAddresses();
                while (addrs.hasMoreElements())
                {
                    ip = addrs.nextElement();
                    if (ip != null
                            && ip instanceof Inet4Address
                            && ip.isSiteLocalAddress()
                            && !ip.getHostAddress().equals(INTRANET_IP))
                    {
                        return ip.getHostAddress();
                    }
                }
            }
    
            // 如果没有外网IP,就返回内网IP
            return INTRANET_IP;
        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    }


    换api:NetworkInterface.getNetworkInterfaces() ,再做过滤,代码如下:

    转载:Java获得系统的外网IP
    https://www.cnblogs.com/kagome2014/p/6428325.html

    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.util.Enumeration;

    /**

    • 系统工具类,用于获取系统相关信息
    • Created by kagome.
      */

    public class CustomSystemUtil {

    public static String INTRANET_IP = getIntranetIp(); // 内网IP
    public static String INTERNET_IP = getInternetIp(); // 外网IP
    
    private CustomSystemUtil(){}
    
    /**
     * 获得内网IP
     * @return 内网IP
     */
    private static String getIntranetIp(){
        try{
            return InetAddress.getLocalHost().getHostAddress();
        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }
    
    /**
     * 获得外网IP
     * @return 外网IP
     */
    private static String getInternetIp(){
        try{
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            Enumeration<InetAddress> addrs;
            while (networks.hasMoreElements())
            {
                addrs = networks.nextElement().getInetAddresses();
                while (addrs.hasMoreElements())
                {
                    ip = addrs.nextElement();
                    if (ip != null
                            && ip instanceof Inet4Address
                            && ip.isSiteLocalAddress()
                            && !ip.getHostAddress().equals(INTRANET_IP))
                    {
                        return ip.getHostAddress();
                    }
                }
            }
    
            // 如果没有外网IP,就返回内网IP
            return INTRANET_IP;
        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    }

    2019-07-17 23:24:39
    赞同 1 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS块储存产品全面解析 立即下载