你可以用这个方法读取你所需要的本机IP地址
=======================================================
J2SE50新特性之ProcessBuilder
这个例子使用了J2SE50的ProcessBuilder类执行外部的程序,相对于 Runtimeexec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址
package comkuaffjdk5package;
import javaioIOException;
import javaioInputStream;
import javautilArrayList;
import javautilList;
public class ProcessBuilderShow
{
public static List getPhysicalAddress()
{
Process p = null;
//物理网卡列表
List address = new ArrayList();
try
{
//执行ipconfig /all命令
p = new ProcessBuilder("ipconfig", "/all")start();
}
catch (IOException e)
{
return address;
}
byte[] b = new byte[1024];
StringBuffer sb = new StringBuffer();
//读取进程输出值
InputStream in = pgetInputStream();
try
{
while (inread(b)>0)
{
sbappend(new String(b));
}
}
catch (IOException e1)
{
}
finally
{
try
{
inclose();
}
catch (IOException e2)
{
}
}
//以下分析输出值,得到物理网卡
String rtValue = sbsubstring(0);
int i = rtValueindexOf("Physical Address :");
while(i>0)
{
rtValue = rtValuesubstring(i + "Physical Address :"length());
addressadd(rtValuesubstring(0,18));
i = rtValueindexOf("Physical Address :");
}
return address;
}
public static void main(String[] args)
{
List address = ProcessBuilderShowgetPhysicalAddress();
for(String add:address)
{
Systemoutprintf("物理网卡地址:%s%n", add);
}
}
}
import javanet;
public class Test {
public static void main(String[] args) throws Exception {
String ip = InetAddressgetLocalHost()getHostAddress();
Systemoutprintln(ip);
}
}
import javanetInet4Address;
import javanetInetAddress;
import javanetNetworkInterface;
import javanetSocketException;
import javautilArrayList;
import javautilEnumeration;
import javautilList;
/
@author Becolette
@description TODO
@date 2015-11-5 下午01:58:46
/
public class IpAddress {
public static String find() {
List<String> ips = new ArrayList<String>();
// 返回所有网络接口的一个枚举实例
Enumeration<> allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterfacegetNetworkInterfaces();
} catch (SocketException e) {
eprintStackTrace();
}
InetAddress ip = null;
while (allNetInterfaceshasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfacesnextElement();
Enumeration<InetAddress> addresses = netInterfacegetInetAddresses();
while (addresseshasMoreElements()) {
// 获得当前网络接口
ip = (InetAddress) addressesnextElement();
if (ip != null && ip instanceof Inet4Address && ipgetHostAddress()indexOf("") != -1) {
ipsadd(ipgetHostAddress());
}
}
}
if (ipssize() == 1) {
return ipsget(0);
} else {
for (String ipa : ips) {
if (!"127001"equals(ipa)) {
return ipa;
}
}
}
return MacAddressfind();
}
}
public static void main(String[] args) {
try {
Systemoutprintln("当前时间:"+new Date());
Systemoutprintln("IP地址 : " + InetAddressgetLocalHost());
} catch (UnknownHostException e) {
eprintStackTrace();
}
}
import javanetInetAddress;
import javanetUnknownHostException;
public class Test
{
public static void main(String[] args) throws UnknownHostException
{
InetAddress inet = InetAddressgetLocalHost();
Systemoutprintln("本机的ip=" + inetgetHostAddress());
}
}
以上就是关于java获得IP地址全部的内容,包括:java获得IP地址、java 如何提取本地IP、如何用java获取本地ip地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)