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