이 기사에서는 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 (private iputil) ) {} / ** *이 메소드는 다음을 설명합니다 : 서버의 IP 주소를 가져옵니다 * @author : @author : [email protected] * @version : 2014 년 9 월 5 일 오후 4:57:15 오후 / public static string getLocalip () {string sip = "; ) {break} networkinterface ni = (NetworkInterface) .nextElement (); ips = ni.getInetAddresses () {ip = (inetAddress) ips.nextElement (); if (! ip.isloopbackaddress () && ip.gethostaddress (). matches ( "(//d {1,3 }//.) {3 }//d {1,3}")) {bfindip = true; break;}}}} catch (exception e) {oututil.error (e.getMessage ()) {sip = ip.gethostAddress (); / ** *이 메소드는 다음을 설명합니다. 서버의 IP 주소 (다중 네트워크 카드) * @Author : [email protected] * @version : 2014 년 9 월 5 일 오후 4시 57 분 15 초 */ 공개 정적 목록 < getLocalips () {inemaddress ip = null; new arraylist <string> () {enumeration <networkinterface>) hasmoreelements () {NetworkInterface ni = (네트워크 인터페이스) .nextElement (); if (! ip. isloopbackaddress () && ip.gethostaddress (). matches ( "(//d {1,3 }//.) {3 }//d {1,3}") {iplist.add (iplist.add) ip.gethostaddress ()}}}} {oututil.error (e.getMessage ()); 서버 * @author : [email protected] * @version : 2014 년 9 월 5 일 오후 5시 */ public static string getMacid () {String macid = "; = null; Boolean bfindip = 거짓; 네트워크 <tetworkinterfaces ) netinterfaces .nextElement (); ------------------------------------------------------ ); while (ip.hasmoreElements ()) {ip = (inetAddress) ifs.nextElement (); d {1,3} //.) {3}//d {1,3} ")))) {bfindip = true; break;}}}} catch (예외 e) {oututil.error (iputil.class, e.getMessage ()} if (null! = ip) {macid = getMacfrombytes (ni.gethardWareaddress ()) {oututil.error (iputil.class, e.getMessage () ;}} return macid} /** *이 방법은 서버의 Mac 주소를 얻습니다. : 25 publy static list <string> getMacids () {inetAddress ip = null <string> null; NE TWININTERFACE>) 네트워크 표면 .getNetWorkInterfaces (); ni.getName을 사용하여 모든 ips 열거 <inetAddress> ips = ni.getInetAdd resses (ips.hasmoreElements (); ip.isloopbackaddress () // non-127.0.0.1 && ip.gethostaddress (). matches ( "(// d {1,3} //.) {3} // d {1,3}") { maclist.add (getmacfrombytes (ni.gethardwareaddress ())}} {oututil.error (iputil.class, e.getmessage ()); BYTE [] BYTER (BYTE BYTE); (B & 240) >> 4); } return mac.toString (). touppercase ()};이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.