この記事では、JavaプログラミングによるサーバーIPアドレスとMacアドレスを取得する方法について説明します。次のように、参照のために共有してください。
テスト済みシステム:
Windows Linux Unix
127.0.0.1や0.0.0.1などの異常なIPを除外します
java.net.inetAddress; Import java.net.networkinterface; import java.net.socketexception; Import java.util.arrayList; Import java.util.en umeration; import java.util.list; public class iputil {private iputil( ){} / ** *このメソッドは説明しています:サーバーのIPアドレスを取得 * @Author:[email protected] * @version:2014年9月5日4:57:15 pm * / public static string getLocalip() {string sip = ""; ){break;} networe interface ni = netInterfaces .numeration <inetadress> ips = ni.getineTadresses(); if(!ip.isloopbackaddress()&& ip.gethostaddress()。matches( "(//d {1,3 }//.) {3 }//d {1,3}"){bfindip = true; BREAK; / ** *このメソッドは説明します:サーバーのIPアドレスを取得(複数のネットワークカード) * @Author:[email protected] * @version:2014年9月5日午後4時57:15 */ public Static List << string> getlocalips(){inemaddress ip = null; hasmoreelements()){networkinterface ni =(networe interface)netInterfaces .nextElement(); if(!ip。isloopbackaddress()&& ip.gethostaddress()。matches( "(//d {1,3 }//.) {3 }//d {1,3}"){iplist.add( ip.gethostadress());サーバー * @author:[email protected] * @version:2014年9月5日 */ public string getmacid(){string macid = ""; = null {boolean bfindip = networtinterfaces =(networkinterface>)while() )netInterfaces .nextElement(); ) d {1,3} //。){3}//d {1,3} ")){bfindip = true; break;}}}}}}}} {oututil.error(iputil.class、 e.getMessage()) 。 :25 pm */ public string> getmacids(){inetaddress ip = null; NEツインインターフェイス>)netwednetworkinterfaces(); ni.getNameを使用して//すべてのIPS列挙<inetadress> ips = ni.getInetadd resses()を実行できます。 ip.isloopbackaddress()// non-127.0.0.1 && ip.gethostaddress()。matches( "(// d {1,3} //。){3} // d {1,3}"){){ MacList.Add(ni.gethardwareaddress()); byte [] stringbuffer mac = byte currentbyte; (B&240)>> 4); } mac.toString()。touppercase()}}この記事がみんなのJavaプログラミングに役立つことを願っています。