マシンのIPおよびMacアドレスを取得するJava
一部のマシンには多くの仮想ネットワークカードがあり、IPアドレスを取得するときにいくつかの事故が発生するため、いくつかの検証が必要です。
// MACアドレスを取得しますpublic Static String getMacAddress(){try {enumeration <NetworkInterface> allnetInterfaces = networkInterface.getNetWorkInterfaces(); byte [] mac = null; while(allnetinterfaces.hasmoreElements()){networkinterface netInterface =(networkinterface)allnetInterfaces.nextelement(); if(netInterface.isloopback()|| netInterface.isvirtual()||!netInterface.isup()){継続; } else {mac = netinterface.gethardwareaddress(); if(mac!= null){stringbuilder sb = new StringBuilder(); for(int i = 0; i <mac.length; i ++){sb.append(string.format( "%02x%s"、mac [i]、(i <mac.length -1)? " - ": "")); } if(sb.length()> 0){return sb.toString(); }}}}}} catch(Exception E){_logger.error( "Macアドレス取得に失敗した"、e); } 戻る ""; } // IPアドレスを取得しますpublic static string getIpAddress(){try {enumeration <NetworkInterface> allnetInterfaces = networkInterface.getNetWorkInterfaces(); inetAddress IP = null; while(allnetinterfaces.hasmoreElements()){networkinterface netInterface =(networkinterface)allnetInterfaces.nextelement(); if(netInterface.isloopback()|| netInterface.isvirtual()||!netInterface.isup()){継続; } else {enumeration <inetaddress> addresses = netInterface.getInetAddresses(); while(addresses.hasmoreElements()){ip = addresses.nextelement(); if(ip!= null && ip instanceof inet4Address){return ip.gethostaddress(); }}}}}} catch(Exception E){_logger.error( "IPアドレスの取得に失敗した"、e); } 戻る ""; }上記のコードで
netinterface.isloopback()|| netInterface.isvirtual()|| !netInterface.isup()
いくつかの非物理的なネットワークカードまたは役に立たないネットワークをうまくフィルタリングし、インターネット上のIPv4アドレスを取得できます。
これについて言えば、一般的に使用されるものがいくつかあります。
1.現在のマシンのオペレーティングシステムを取得します
public final static string win_os = "windows"; public final static string mac_os = "mac"; public final static string linux_os = "linux"; public final static string other_os = "other"; public static string getos(){if(systemutils.is_os_windows){return win_os; } if(systemutils.is_os_mac || systemutils.is_os_mac_osx){return mac_os; } if(systemutils.is_os_unix){return linux_os; } return other_os; } 2. HTTPアクセスプロキシを設定します
/ *** set http proxy*/ public static void sethttpproxy(){properties prop = system.getProperties(); // httpを設定して、使用するプロキシサーバーのアドレスにアクセスするprop.setproperty( "http.proxyhost"、http_proxy_host); // HTTPを設定して、使用するプロキシサーバーのポートにアクセスします。 //プロキシサーバーを介してアクセスする必要のないホストを設定することは *ワイルドカード文字を使用でき、複数のアドレスは|で区切られます。 prop.setProperty( "http.nonproxyhosts"、remoteconfig.proxt_filter_domain); } / *** http proxyを削除* / public static void removehtpproxy(){properties prop = system.getProperties(); Prop.Remove( "http.proxyhost"); Prop.Remove( "http.proxyport"); Prop.Remove( "http.nonproxyhost"); Prop.Remove( "http.nonproxyhost"); }アプリケーションが起動したら、HTTPリクエストにアクセスする前に設定するだけです。もちろん、http.nonproxyhostは設定せずに設定できます。つまり、すべてのHTTP要求がプロキシされています。
HTTPSプロキシについては、次のように設定できます。
System.setProperty( "https.proxyhost"、 "http_proxy_host");
System.SetProperty( "https.proxyport"、 "http_proxy_port");
上記は、JavaがネイティブIPとMacを取得する例です。困っている友達はそれを参照できます。このサイトへのご支援ありがとうございます!