جافا للحصول على عنوان IP و MAC للجهاز
تحتوي بعض الآلات على العديد من بطاقات الشبكة الافتراضية ، وسوف تحدث بعض الحوادث عند الحصول على عناوين IP ، لذلك هناك حاجة إلى بعض التحقق:
// احصل على عنوان MAC Static Static Static GetMacAddress () {try {enumeration <NetworkInterface> allNetInterFaces = networkInterface.getNetworkInterFaces () ؛ بايت [] mac = null ؛ بينما (allnetinterfaces.hasmoreElements ()) {networkInterface netInterface = (NetworkInterface) allNetInterFaces.nextElement () ؛ if (netInterface.isLoopBack () || netInterface.isvirtual () ||! netInterface.isup ()) {extre } آخر {mac = netInterface.gethardWareadDress () ؛ if (mac! = null) {StringBuilder sb = new StringBuilder () ؛ لـ (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 (استثناء e) {_logger.error ("فشل الحصول على عنوان MAC" ، e) ؛ } يعود ""؛ } // احصل على عنوان IP static static getipaddress () {try {enumeration <NetworkInterface> allNetInterFaces = networkInterface.getNetworkInterFaces () ؛ inetaddress IP = null ؛ بينما (allnetinterfaces.hasmoreElements ()) {networkInterface netInterface = (NetworkInterface) allNetInterFaces.nextElement () ؛ if (netInterface.isLoopBack () || netInterface.isvirtual () ||! netInterface.isup ()) {extre } else {enumeration <InetAddress> adgens = netInterface.getInetAddresses () ؛ بينما (addresses.hasmoreElements ()) {ip = address.nextElement () ؛ if (ip! = null && ip extryof inet4address) {return ip.gethostaddress () ؛ }}}}} catch (استثناء e) {_logger.error ("فشل الحصول على عنوان IP" ، e) ؛ } يعود ""؛ } في الرمز أعلاه
netInterface.IsloOpback () || netInterface.isvirtual () || ! netInterface.isup ()
يمكنه تصفية بعض بطاقات الشبكة غير المادية أو شبكات عديمة الفائدة جيدًا ، ثم استرداد عنوان IPv4 على الإنترنت.
الحديث عن هذا ، هناك بعض تلك الشائعة الاستخدام:
1. الحصول على نظام التشغيل للجهاز الحالي
السلسلة الثابتة النهائية العامة Win_os = "Windows" ؛ السلسلة الثابتة النهائية العامة mac_os = "mac" ؛ السلسلة الثابتة النهائية العامة Linux_OS = "Linux" ؛ السلسلة الثابتة النهائية العامة other_os = "others" ؛ السلسلة الثابتة العامة 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 ؛ } إرجاع 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.proxyport" ، http_proxy_port) ؛ // إعداد مضيف لا يلزم الوصول إليه من خلال خادم وكيل يمكنه استخدام حرف * Wildcard ، ويتم فصل عناوين متعددة بواسطة | مع prop.setProperty ("http.nonproxyhosts" ، remoteconfig.proxt_filter_domain) ؛ } / *** إزالة HTTP Proxy* / public static void removeHttProxy () {properties prop = system.getProperties () ؛ prop.remove ("http.proxyhost") ؛ prop.remove ("http.proxyport") ؛ prop.remove ("http.nonproxyhost") ؛ prop.remove ("http.nonproxyhost") ؛ } عند بدء تشغيل التطبيق ، ما عليك سوى تعيينه قبل الوصول إلى طلب HTTP. بالطبع ، يمكن تعيين http.nonproxyhosts دون إعداد ، مما يعني أن جميع طلبات HTTP يتم وكيل.
أما بالنسبة لوكيل HTTPS ، يمكنك تعيينه مثل هذا:
System.SetProperty ("https.proxyhost" ، "http_proxy_host") ؛
System.SetProperty ("https.proxyport" ، "http_proxy_port") ؛
ما سبق هو مثال على الحصول على جافا الحصول على IP الأصلي و MAC. يمكن للأصدقاء المحتاجين الرجوع إليه. شكرا لك على دعمك لهذا الموقع!