精华内容
下载资源
问答
  • Java获取本地服务器IP

    千次阅读 2017-12-02 09:45:26
    Java获取本地服务器IP,我们最简单的写法: [java] view plain copy import java.net.InetAddress;  public class CatchIp{   public static void main(String[] args)  ...

    Java获取本地服务器IP,我们最简单的写法:

    [java] view plain copy
    1. import java.net.InetAddress;    
    2. public class CatchIp{  
    3.  public static void main(String[] args)  
    4.     {  
    5.         try  
    6.         {  
    7.             // 获取IP地址  
    8.             String ip = InetAddress.getLocalHost().getHostAddress();  
    9.          
    10.             System.out.println("IP地址:"+ip);  
    11.         }  
    12.         catch (Exception e)  
    13.         {  
    14.             System.out.println("异常:" + e);  
    15.             e.printStackTrace();  
    16.         }  
    17.     }  
    18.     }  

    但是这样有个问题,我们在windows系统,我们可以获取正确的IP

    但是放在linux,可能是获取的IP地址为:127.0.0.1

    而且在生产系统中,一台服务器可能有很多张网卡,这样获取ip比较麻烦,具体要了解生产系统服务器的IP地址我们可以参考这个网址:

    http://blog.163.com/abkiss@126/blog/static/3259410020117493943770/?suggestedreading

    代码如下:

    [java] view plain copy
    1. /* 
    2.  * To change this template, choose Tools | Templates 
    3.  * and open the template in the editor. 
    4.  */  
    5. package org.lib.com;  
    6.   
    7. /** 
    8.  * 
    9.  * @author Administrator 
    10.  */  
    11. import java.io.BufferedReader;  
    12. import java.io.IOException;  
    13. import java.io.InputStreamReader;  
    14. import java.net.Inet6Address;  
    15. import java.net.InetAddress;  
    16. import java.net.NetworkInterface;  
    17. import java.net.SocketException;  
    18. import java.net.UnknownHostException;  
    19. import java.util.ArrayList;  
    20. import java.util.Enumeration;  
    21. import java.util.List;  
    22.        
    23. public class TestAddr {  
    24.        
    25.         /** 
    26.          * 获取本机所有IP 
    27.          */  
    28.     private static String[] getAllLocalHostIP() {  
    29.         List<String> res = new ArrayList<String>();  
    30.         Enumeration netInterfaces;  
    31.         try {  
    32.             netInterfaces = NetworkInterface.getNetworkInterfaces();  
    33.             InetAddress ip = null;  
    34.             while (netInterfaces.hasMoreElements()) {  
    35.                 NetworkInterface ni = (NetworkInterface) netInterfaces  
    36.                         .nextElement();  
    37.                 System.out.println("---Name---:" + ni.getName());  
    38.                 Enumeration nii = ni.getInetAddresses();  
    39.                 while (nii.hasMoreElements()) {  
    40.                     ip = (InetAddress) nii.nextElement();  
    41.                     if (ip.getHostAddress().indexOf(":") == -1) {  
    42.                         res.add(ip.getHostAddress());  
    43.                         System.out.println("本机的ip=" + ip.getHostAddress());  
    44.                     }  
    45.                 }  
    46.             }  
    47.         } catch (SocketException e) {  
    48.             e.printStackTrace();  
    49.             }  
    50.             return (String[]) res.toArray(new String[0]);  
    51.         }  
    52.     public static String getLocalIP() {  
    53.            String ip = "";  
    54.            try {  
    55.                    Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();  
    56.                    while (e1.hasMoreElements()) {  
    57.                        NetworkInterface ni = (NetworkInterface) e1.nextElement();  
    58.                         System.out.println ("getLocalIP--nic.getDisplayName ():" + ni.getDisplayName ());   
    59.                     System.out.println ("getLocalIP--nic.getName ():" + ni.getName ());  
    60.                        if (!ni.getName().equals("eth0")) {  
    61.                            continue;  
    62.                        } else {  
    63.                        Enumeration<?> e2 = ni.getInetAddresses();  
    64.                            while (e2.hasMoreElements()) {  
    65.                            InetAddress ia = (InetAddress) e2.nextElement();  
    66.                            if (ia instanceof Inet6Address)  
    67.                                continue;  
    68.                            ip = ia.getHostAddress();  
    69.                        }  
    70.                            break;  
    71.                        }  
    72.                    }  
    73.                } catch (SocketException e) {  
    74.                    e.printStackTrace();  
    75.                    System.exit(-1);  
    76.                }  
    77.                return ip;  
    78.            }  
    79.         public static String getWinLocalIP ()   
    80.             {   
    81.                 String ip = "";   
    82.                 try   
    83.                 {   
    84.                     Enumeration <?> e1 = (Enumeration <?>) NetworkInterface.getNetworkInterfaces ();   
    85.                     while (e1.hasMoreElements ())   
    86.                     {   
    87.                         NetworkInterface ni = (NetworkInterface) e1.nextElement ();  
    88.                         System.out.println ("getWinLocalIP--nic.getDisplayName ():" + ni.getDisplayName ());   
    89.                         System.out.println ("getWinLocalIP--nic.getName ():" + ni.getName ());  
    90.                         Enumeration <?> e2 = ni.getInetAddresses ();   
    91.                         while (e2.hasMoreElements ())   
    92.                         {   
    93.                             InetAddress ia = (InetAddress) e2.nextElement ();   
    94.                             ip = ia.getHostAddress ();   
    95.                     }   
    96.                     }   
    97.                 }   
    98.                 catch (SocketException e)   
    99.                 {   
    100.                     e.printStackTrace ();   
    101.                 System.exit (-1);   
    102.                 }   
    103.                 return ip;   
    104.             }  
    105.         /** 
    106.          * 获取本机所有物理地址 
    107.          * 
    108.          * @return 
    109.          */  
    110.         public static String getMacAddress() {  
    111.         String mac = "";  
    112.         String line = "";  
    113.    
    114.         String os = System.getProperty("os.name");  
    115.   
    116.        if (os != null && os.startsWith("Windows")) {  
    117.             try {  
    118.                     String command = "cmd.exe /c ipconfig /all";  
    119.                     Process p = Runtime.getRuntime().exec(command);  
    120.        
    121.                     BufferedReader br = new BufferedReader(new InputStreamReader(p  
    122.                             .getInputStream()));  
    123.        
    124.                     while ((line = br.readLine()) != null) {  
    125.                         if (line.indexOf("Physical Address") > 0) {  
    126.                             int index = line.indexOf(":") + 2;  
    127.        
    128.                             mac = line.substring(index);  
    129.        
    130.                             break;  
    131.                         }  
    132.                     }  
    133.        
    134.                     br.close();  
    135.        
    136.                 } catch (IOException e) {  
    137.                 }  
    138.             }  
    139.        
    140.             return mac;  
    141.         }  
    142.        
    143.         public String getMacAddress(String host) {  
    144.             String mac = "";  
    145.             StringBuffer sb = new StringBuffer();  
    146.        
    147.             try {  
    148.                 NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress  
    149.                         .getByName(host));  
    150.        
    151.                 // byte[] macs = ni.getHardwareAddress();  
    152.        
    153.                 // for (int i = 0; i < macs.length; i++) {  
    154.                 // mac = Integer.toHexString(macs[i] & 0xFF);  
    155.                 //  
    156.                 // if (mac.length() == 1) {  
    157.                 // mac = '0' + mac;  
    158.                 // }  
    159.                 //  
    160.                 // sb.append(mac + "-");  
    161.                 // }  
    162.        
    163.             } catch (SocketException e) {  
    164.                 e.printStackTrace();  
    165.             } catch (UnknownHostException e) {  
    166.                 e.printStackTrace();  
    167.             }  
    168.        
    169.             mac = sb.toString();  
    170.             mac = mac.substring(0, mac.length() - 1);  
    171.        
    172.             return mac;  
    173.     }  
    174.        
    175.         /** 
    176.          * @param args 
    177.          */  
    178.         public static void main(String[] args) {  
    179.             TestAddr.getAllLocalHostIP();  
    180.             System.out.println("LocalIP:"+TestAddr.getLocalIP());  
    181.             System.out.println("getWinLocalIP:"+TestAddr.getWinLocalIP());  
    182.             System.out.println(TestAddr.getMacAddress());  
    183.         }  
    184.        
    185.     }  
    当然 ,我们要是想获取服务器的唯一信息,经常有个偷懒的做法,那就是获取服务器的hostname,即主机名,写法也比较统一简单:

    [java] view plain copy
    1. import java.net.InetAddress;    
    2. public class CatchHostname{  
    3.  public static void main(String[] args)  
    4.     {  
    5.         try  
    6.         {  
    7.             // 获取计算机名  
    8.             String name = InetAddress.getLocalHost().getHostName();  
    9.             System.out.println("计算机名:"+name);  
    10.            
    11.         }  
    12.         catch (Exception e)  
    13.         {  
    14.             System.out.println("异常:" + e);  
    15.             e.printStackTrace();  
    16.         }  
    17.     }  
    18.     }  
    展开全文
  • 关于Java获取本地服务器IP的问题

    千次阅读 2015-03-01 19:29:50
    Java获取本地服务器IP,我们最简单的写法: import java.net.InetAddress; public class CatchIp{ public static void main(String[] args) { try { // 获取IP地址 String ip = InetAd

    Java获取本地服务器IP,我们最简单的写法:

    import java.net.InetAddress;  
    public class CatchIp{
     public static void main(String[] args)
        {
            try
            {
                // 获取IP地址
                String ip = InetAddress.getLocalHost().getHostAddress();
           
                System.out.println("IP地址:"+ip);
            }
            catch (Exception e)
            {
                System.out.println("异常:" + e);
                e.printStackTrace();
            }
        }
    	}

    但是这样有个问题,我们在windows系统,我们可以获取正确的IP

    但是放在linux,可能是获取的IP地址为:127.0.0.1

    而且在生产系统中,一台服务器可能有很多张网卡,这样获取ip比较麻烦,具体要了解生产系统服务器的IP地址我们可以参考这个网址:

    http://blog.163.com/abkiss@126/blog/static/3259410020117493943770/?suggestedreading

    代码如下:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package org.lib.com;
    
    /**
     *
     * @author Administrator
     */
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Inet6Address;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.List;
         
    public class TestAddr {
         
            /**
             * 获取本机所有IP
             */
        private static String[] getAllLocalHostIP() {
            List<String> res = new ArrayList<String>();
            Enumeration netInterfaces;
            try {
                netInterfaces = NetworkInterface.getNetworkInterfaces();
                InetAddress ip = null;
                while (netInterfaces.hasMoreElements()) {
                    NetworkInterface ni = (NetworkInterface) netInterfaces
                            .nextElement();
                    System.out.println("---Name---:" + ni.getName());
                    Enumeration nii = ni.getInetAddresses();
                    while (nii.hasMoreElements()) {
                        ip = (InetAddress) nii.nextElement();
                        if (ip.getHostAddress().indexOf(":") == -1) {
                            res.add(ip.getHostAddress());
                            System.out.println("本机的ip=" + ip.getHostAddress());
                        }
                    }
                }
            } catch (SocketException e) {
                e.printStackTrace();
                }
                return (String[]) res.toArray(new String[0]);
            }
        public static String getLocalIP() {
               String ip = "";
               try {
                       Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
                       while (e1.hasMoreElements()) {
                           NetworkInterface ni = (NetworkInterface) e1.nextElement();
                            System.out.println ("getLocalIP--nic.getDisplayName ():" + ni.getDisplayName ()); 
                        System.out.println ("getLocalIP--nic.getName ():" + ni.getName ());
                           if (!ni.getName().equals("eth0")) {
                               continue;
                           } else {
                           Enumeration<?> e2 = ni.getInetAddresses();
                               while (e2.hasMoreElements()) {
                               InetAddress ia = (InetAddress) e2.nextElement();
                               if (ia instanceof Inet6Address)
                                   continue;
                               ip = ia.getHostAddress();
                           }
                               break;
                           }
                       }
                   } catch (SocketException e) {
                       e.printStackTrace();
                       System.exit(-1);
                   }
                   return ip;
               }
            public static String getWinLocalIP () 
                { 
                    String ip = ""; 
                    try 
                    { 
                        Enumeration <?> e1 = (Enumeration <?>) NetworkInterface.getNetworkInterfaces (); 
                        while (e1.hasMoreElements ()) 
                        { 
                            NetworkInterface ni = (NetworkInterface) e1.nextElement ();
                            System.out.println ("getWinLocalIP--nic.getDisplayName ():" + ni.getDisplayName ()); 
                            System.out.println ("getWinLocalIP--nic.getName ():" + ni.getName ());
                            Enumeration <?> e2 = ni.getInetAddresses (); 
                            while (e2.hasMoreElements ()) 
                            { 
                                InetAddress ia = (InetAddress) e2.nextElement (); 
                                ip = ia.getHostAddress (); 
                        } 
                        } 
                    } 
                    catch (SocketException e) 
                    { 
                        e.printStackTrace (); 
                    System.exit (-1); 
                    } 
                    return ip; 
                }
            /**
             * 获取本机所有物理地址
             *
             * @return
             */
            public static String getMacAddress() {
            String mac = "";
            String line = "";
     
            String os = System.getProperty("os.name");
    
           if (os != null && os.startsWith("Windows")) {
                try {
                        String command = "cmd.exe /c ipconfig /all";
                        Process p = Runtime.getRuntime().exec(command);
         
                        BufferedReader br = new BufferedReader(new InputStreamReader(p
                                .getInputStream()));
         
                        while ((line = br.readLine()) != null) {
                            if (line.indexOf("Physical Address") > 0) {
                                int index = line.indexOf(":") + 2;
         
                                mac = line.substring(index);
         
                                break;
                            }
                        }
         
                        br.close();
         
                    } catch (IOException e) {
                    }
                }
         
                return mac;
            }
         
            public String getMacAddress(String host) {
                String mac = "";
                StringBuffer sb = new StringBuffer();
         
                try {
                    NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress
                            .getByName(host));
         
                    // byte[] macs = ni.getHardwareAddress();
         
                    // for (int i = 0; i < macs.length; i++) {
                    // mac = Integer.toHexString(macs[i] & 0xFF);
                    //
                    // if (mac.length() == 1) {
                    // mac = '0' + mac;
                    // }
                    //
                    // sb.append(mac + "-");
                    // }
         
                } catch (SocketException e) {
                    e.printStackTrace();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
         
                mac = sb.toString();
                mac = mac.substring(0, mac.length() - 1);
         
                return mac;
        }
         
            /**
             * @param args
             */
            public static void main(String[] args) {
                TestAddr.getAllLocalHostIP();
                System.out.println("LocalIP:"+TestAddr.getLocalIP());
                System.out.println("getWinLocalIP:"+TestAddr.getWinLocalIP());
                System.out.println(TestAddr.getMacAddress());
            }
         
        }
    当然 ,我们要是想获取服务器的唯一信息,经常有个偷懒的做法,那就是获取服务器的hostname,即主机名,写法也比较统一简单:

    import java.net.InetAddress;  
    public class CatchHostname{
     public static void main(String[] args)
        {
            try
            {
                // 获取计算机名
                String name = InetAddress.getLocalHost().getHostName();
                System.out.println("计算机名:"+name);
             
            }
            catch (Exception e)
            {
                System.out.println("异常:" + e);
                e.printStackTrace();
            }
        }
    	}


    展开全文
  • java获取本地服务器ip和端口

    千次阅读 2018-11-13 11:35:22
    1、Spring: @Autowired protected HttpServletRequest request; ...System.out.println(request.getLocalAddr()+":"+request.getLocalPort());...HttpServletRequest request=ServletAction...

    1、Spring:

        @Autowired
        protected HttpServletRequest request;
    
    
    
    
    System.out.println(request.getLocalAddr()+":"+request.getLocalPort());

    2、Struts2:

    HttpServletRequest request=ServletActionContext.getRequest();
    
    System.out.println(request.getLocalAddr()+":"+request.getLocalPort());
    
    

     

     


     

    展开全文
  • @SuppressWarnings("rawtypes") ...private static String getHostIp() { Enumeration allNetInterfaces = null; try { allNetInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e
    @SuppressWarnings("rawtypes")
    private static String getHostIp() {
    Enumeration allNetInterfaces = null;
    try {
    allNetInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    InetAddress ip = null;
    String hostIP = "";
    while (allNetInterfaces.hasMoreElements()) {
    NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
    System.out.println(netInterface.getName());
    Enumeration addresses = netInterface.getInetAddresses();
    while (addresses.hasMoreElements()) {
    ip = (InetAddress) addresses.nextElement();
    if (ip != null && ip instanceof Inet4Address) {
    hostIP = ip.getHostAddress();
    System.out.println("本机的IP = " + ip.getHostAddress());
    }
    }
    }
    return hostIP;
    }
    展开全文
  • java获取本地IP和服务器IP

    万次阅读 2017-07-24 10:55:34
    java获取IP地址最近公司在做日志报警服务,需要在异常信息上添加IP地址,用于快速定位异常位置。总结以下知识点: 获取本地IP地址 String address = InetAddress.getLocalHost().getHostAddress().toString(); 这种...
  • 想了解java获取linux服务器上的IP操作的相关内容吗,gzy_0412在本文为您仔细讲解java linux服务器IP的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,linux服务器,IP,下面大家一起来学习吧。...
  • java"] System.out.println("getRequestURL:"+request.getRequestURL()); System.out.println("getLocalAddr:"+request.getLocalAddr()); System.out.println("getLocalPort:...
  • 以前把程式发布的Server IP都写成一个IP-Config 文件中。 为了避免发布程式每次去更改IP-Config ,或是在...即自动获取服务器IP。看了很多function,比如   String serverIP = ServletActionContext.getReq...
  • 获取本地服务器IP经常会出现127.0.0.1,0:0:0:0:0:0:0:1,fe80:0:0:0:960:74bd:e1a0:e5b9%11 这些情况,以下代码可解决此问题public static void main(String[] args) {try {Enumeration enumeration = Network...
  •  在项目中经常会遇到需要获取服务器的IP和本地IP,而我们大部分的服务器都配置在linux上... //取得服务器Ip  public static String getSerIp(){ String clientIp =""; // 根据网卡取本机配置的IP...
  • java获取linux服务器IP

    千次阅读 2019-10-26 09:24:31
    在项目开发过程中,需要获取linux服务器ip作为zookeeper节点名称,原先使用的是以下的方法: public static String getIpAddress() { String hostAddress = ""; try { InetAddress address = InetAddress....
  • Java获取当前服务器IP

    2018-07-06 10:23:00
    package hope.ipaddress.demo; import java.net.InetAddress; import java.net.NetworkInterface; ...import java.net.SocketException;...import java.util.Enumeration;... * 获取当前服务器IP * @author hp ...
  • java.net.InetAddress类 ...InetAddress是JavaIP地址的封装,在java.net中有许多类都使用到了InetAddress, 包括ServerSocket,Socket,DatagramSocket等等。 InetAddress的实例对象包含以数字形式保存...
  • Java中,可以使用InetAddress.getLocalHost()来获取运行Java应用程序的当前服务器IP地址。 package com.mkyong; import java.net.InetAddress; import java.net.UnknownHostException; public class Test { ...
  • java获取本地ip地址(Windows和Linux通用)

    千次阅读 2019-04-24 17:11:57
    package ... import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration;...* 获取本地ip地址的方法(Linux环境和Java通用) * @ver...
  • java 获得本地IP和远程服务器IP

    千次阅读 2016-07-26 16:41:34
    import java.io.IOException; import java.net.InetAddress; import java.net..../*获取IP地址*/ public class CatchIp { private InetAddress LocalIP = null; private InetAddress ServerIP = n
  • 获取当前服务器IP地址 import java.net.*; import java.util.*; //获取当前服务器IP public class IPdemo { public static void main(String[] args) { System.out.println(getMyIp()); } @SuppressWarnings...
  • java 获取服务器ip

    千次阅读 2017-09-14 11:38:48
    public static String getLocalhostIP(){ InetAddress address = null;//获取的是本地IP地址 try { address = InetAddress.getLocalHost(); return address.getHostAddress();//192
  • 主要介绍了Java获取服务器IP及端口的方法,结合实例形式分析了java针对客户端及服务器端各种常见的信息操作技巧与注意事项,需要的朋友可以参考下
  • java 获取本地系统IP 及网咯IP

    千次阅读 2018-01-05 18:11:44
    public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost...//获取的是本地IP地址 //PC-20140317PXKX/192.168.0.121 String hostAddress = address.getHostAddress();//
  • public static String getIP() { String ip = "http://pv.sohu.com/cityjson?ie=utf-8"; String inputLine = ""; String read = ""; String toIp=""; try { ...
  • HttpServletRequest httpRequest=(HttpServletRequest)request;...String strBackUrl = "http://" + request.getServerName() //服务器地址 + ":" + request.getServerPort() //端口号 + httpRequest.
  • 1、tomcat是一款免费的开源Web服务器,如果部署在本地,那么对应的那么为localhost,对应地址为127.0.0.1。 ... 如果部署在服务器(linux)系统类,则... 获取本地Ip地址: public static void main(String[] a...
  • java"] package com; import java.net.InetAddress; import java.net.UnknownHostException; public class Netool { InetAddress myip=null; InetAddress myserver=null; /** * @param .....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 101,117
精华内容 40,446
关键字:

java获取本地服务器ip

java 订阅