Java擷取系統IP地址

來源:互聯網
上載者:User

標籤:擷取ip

在一個項目中如果你想擷取系統的ip地址那麼可能許多同學在想那是多麼容易,但是如果在多種作業系統貌似就不那麼簡單了,下面看看怎麼擷取系統的ip地址的。
package easyway.tbs.commons;  import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; import java.util.Properties; import org.apache.log4j.Logger; /** *  * 本機系統資訊 * @author longgangbai * */ public final class SystemHelper {     private static final Logger logger = Logger.getLogger(SystemHelper.class);      //獲得系統屬性集       public static Properties props=System.getProperties();     //作業系統名稱     public static String OS_NAME=getPropertery("os.name");     //行分頁符     public static String OS_LINE_SEPARATOR=getPropertery("line.separator");     //檔案分隔字元號     public static String OS_FILE_SEPARATOR=getPropertery("file.separator");          /**     *      * 根據系統的類型擷取本伺服器的ip地址     *      * InetAddress inet = InetAddress.getLocalHost();     * 但是上述代碼在Linux下返回127.0.0.1。     * 主要是在linux下返回的是/etc/hosts中配置的localhost的ip地址,     * 而不是網卡的綁定地址。後來改用網卡的綁定地址,可以取到原生ip地址:):     * @throws UnknownHostException      */     public static InetAddress getSystemLocalIp() throws UnknownHostException{         InetAddress inet=null;         String osname=getSystemOSName();         try {             //針對window系統             if(osname.equalsIgnoreCase("Windows XP")){                     inet=getWinLocalIp();             //針對linux系統             }else if(osname.equalsIgnoreCase("Linux")){                     inet=getUnixLocalIp();             }             if(null==inet){                 throw new UnknownHostException("主機的ip地址未知");             }         }catch (SocketException e) {             logger.error("擷取本機ip錯誤"+e.getMessage());             throw new UnknownHostException("擷取本機ip錯誤"+e.getMessage());         }         return inet;     }     /**     * 擷取FTP的配置作業系統     * @return     */     public static String getSystemOSName() {          //獲得系統屬性集           Properties props=System.getProperties();         //作業系統名稱         String osname=props.getProperty("os.name");           if(logger.isDebugEnabled()){             logger.info("the ftp client system os Name "+osname);         }         return osname;     }     /**     * 擷取屬性的值     * @param propertyName     * @return     */     public static String getPropertery(String propertyName){         return props.getProperty(propertyName);     }               /**     * 擷取window 本地ip地址     * @return     * @throws UnknownHostException     */     private static InetAddress getWinLocalIp() throws UnknownHostException{         InetAddress inet = InetAddress.getLocalHost();              System.out.println("原生ip=" + inet.getHostAddress());           return inet;     }     /**     *      * 可能多多個ip地址只擷取一個ip地址     * 擷取Linux 本地IP地址     * @return     * @throws SocketException      */     private static InetAddress getUnixLocalIp() throws SocketException{             Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();             InetAddress ip = null;              while(netInterfaces.hasMoreElements())                  {                      NetworkInterface ni= (NetworkInterface)netInterfaces.nextElement();                      ip=(InetAddress) ni.getInetAddresses().nextElement();                      if( !ip.isSiteLocalAddress()                              && !ip.isLoopbackAddress()                              && ip.getHostAddress().indexOf(":")==-1)                      {                          return ip;                 }                      else                     {                          ip=null;                      }                  }            return null;     }     /**     *      * 擷取當前運行程式的記憶體資訊     * @return     */     public static final String getRAMinfo() {         Runtime rt = Runtime.getRuntime();         return "RAM: " + rt.totalMemory() + " bytes total, " + rt.freeMemory() + " bytes free.";     } }

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java擷取系統IP地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.