代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
获取本机所有ip地址和mac地址
作者:
sdcool
/ 发布于
2014/1/28
/
605
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.lib.com; /** * * @author Administrator */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; public class TestAddr { /** * 获取本机所有IP */ private static String[] getAllLocalHostIP() { List<String> res = new ArrayList<String>(); Enumeration netInterfaces; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement(); System.out.println("---Name---:" + ni.getName()); Enumeration nii = ni.getInetAddresses(); while (nii.hasMoreElements()) { ip = (InetAddress) nii.nextElement(); if (ip.getHostAddress().indexOf(":") == -1) { res.add(ip.getHostAddress()); System.out.println("本机的ip=" + ip.getHostAddress()); } } } } catch (SocketException e) { e.printStackTrace(); } return (String[]) res.toArray(new String[0]); } public static String getLocalIP() { String ip = ""; try { Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces(); while (e1.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) e1.nextElement(); System.out.println ("getLocalIP--nic.getDisplayName ():" + ni.getDisplayName ()); System.out.println ("getLocalIP--nic.getName ():" + ni.getName ()); if (!ni.getName().equals("eth0")) { continue; } else { Enumeration<?> e2 = ni.getInetAddresses(); while (e2.hasMoreElements()) { InetAddress ia = (InetAddress) e2.nextElement(); if (ia instanceof Inet6Address) continue; ip = ia.getHostAddress(); } break; } } } catch (SocketException e) { e.printStackTrace(); System.exit(-1); } return ip; } public static String getWinLocalIP () { String ip = ""; try { Enumeration <?> e1 = (Enumeration <?>) NetworkInterface.getNetworkInterfaces (); while (e1.hasMoreElements ()) { NetworkInterface ni = (NetworkInterface) e1.nextElement (); System.out.println ("getWinLocalIP--nic.getDisplayName ():" + ni.getDisplayName ()); System.out.println ("getWinLocalIP--nic.getName ():" + ni.getName ()); Enumeration <?> e2 = ni.getInetAddresses (); while (e2.hasMoreElements ()) { InetAddress ia = (InetAddress) e2.nextElement (); ip = ia.getHostAddress (); } } } catch (SocketException e) { e.printStackTrace (); System.exit (-1); } return ip; } /** * 获取本机所有物理地址 * * @return */ public static String getMacAddress() { String mac = ""; String line = ""; String os = System.getProperty("os.name"); if (os != null && os.startsWith("Windows")) { try { String command = "cmd.exe /c ipconfig /all"; Process p = Runtime.getRuntime().exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(p .getInputStream())); while ((line = br.readLine()) != null) { if (line.indexOf("Physical Address") > 0) { int index = line.indexOf(":") + 2; mac = line.substring(index); break; } } br.close(); } catch (IOException e) { } } return mac; } public String getMacAddress(String host) { String mac = ""; StringBuffer sb = new StringBuffer(); try { NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress .getByName(host)); // byte[] macs = ni.getHardwareAddress(); // for (int i = 0; i < macs.length; i++) { // mac = Integer.toHexString(macs[i] & 0xFF); // // if (mac.length() == 1) { // mac = '0' + mac; // } // // sb.append(mac + "-"); // } } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } mac = sb.toString(); mac = mac.substring(0, mac.length() - 1); return mac; } /** * @param args */ public static void main(String[] args) {
试试其它关键字
所有ip地址
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
sdcool
贡献的其它代码
(
3
)
.
改变字间距
.
判断变量是否存在
.
获取本机所有ip地址和mac地址
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3