Java Get system IP address

Source: Internet
Author: User
Tags file separator getmessage ftp client

In a project if you want to get the IP address of the system then maybe many students are thinking how easy it is, but if it seems to be less simple in a variety of operating systems, let's look at how to get the IP address of the system.
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; /** * * Native System Information * @author LONGGANGBAI * */Public final class Systemhelper {private static final Logger Logger = Logg      Er.getlogger (Systemhelper.class);     Get system attribute set public static Properties props=system.getproperties ();     Operating system name public static String os_name=getpropertery ("Os.name");     Row page break public static String os_line_separator=getpropertery ("Line.separator");          File separator symbol public static String os_file_separator=getpropertery ("File.separator");     /** * * Obtain the IP address of the server according to the type of system * * inetaddress inet = Inetaddress.getlocalhost ();     * But the above code returns 127.0.0.1 under Linux. * Mainly returned under Linux is the IP address of the localhost configured in/etc/hosts, * instead of the NIC's binding address. Later use the binding address of the network card, you can fetch the IP address of the machine: * @throws unknownhostexception      */public static inetaddress Getsystemlocalip () throws unknownhostexception{inetaddress Inet=null;         String Osname=getsystemosname (); try {//For Window System if (Osname.equalsignorecase ("Windows XP")) {inet=getwinlocal             Ip ();             For Linux Systems}else if (osname.equalsignorecase ("Linux")) {inet=getunixlocalip ();             } if (null==inet) {throw new Unknownhostexception ("The IP address of the host is unknown");             }}catch (SocketException e) {logger.error ("Get native IP Error" +e.getmessage ());         throw new Unknownhostexception ("Get native IP Error" +e.getmessage ());     } return inet;           /** * Get FTP Configuration operating system * @return */public static String Getsystemosname () {//Get system Property Set         Properties props=system.getproperties ();           Operating system name String Osname=props.getproperty ("Os.name"); if (logger.isdebugenabled () {Logger.info ("The FTP client system OS Name" +osname);     } return osname; /** * Gets the value of the property * @param propertyname * @return * */public static string Getpropertery (string proper     Tyname) {return props.getproperty (PropertyName); }/** * Get window Local IP address * @return * @throws unknownhostexception */private static Ineta              Ddress Getwinlocalip () throws unknownhostexception{inetaddress inet = Inetaddress.getlocalhost ();           System.out.println ("ip= of this Machine" + inet.gethostaddress ());     return inet; }/** * * may have multiple IP addresses only get one IP address * Get the Linux local IP address * @return * @throws socketexception */pri vate static InetAddress Getunixlocalip () throws socketexception{enumeration<networkinterface> NetInterf             Aces = 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;  /** * * Gets the current running program's memory information * @return */public static final String Getraminfo () {Runtime RT         = Runtime.getruntime (); Return "RAM:" + rt.totalmemory () + "bytes Total," + rt.freememory () + "bytes Free";}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Get system IP address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.