「java过去本机ip地址」java ip地址
今天给各位分享java过去本机ip地址的知识,其中也会对java ip地址进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java中如何过去本机ip地址并输出
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString;//获得本机IP
address=addr.getHostName()toString;//获得本机名称
java获得IP地址
下面有一篇文章,介绍若何读取物理网卡的地址 ,同样的
你可以用这个方法读取你所需要的本机IP地址
=======================================================
J2SE5.0新特性之ProcessBuilder
这个例子使用了J2SE5.0的ProcessBuilder类执行外部的程序,相对于 Runtime.exec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址
package com.kuaff.jdk5package;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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 = p.getInputStream();
try
{
while (in.read(b)0)
{
sb.append(new String(b));
}
}
catch (IOException e1)
{
}
finally
{
try
{
in.close();
}
catch (IOException e2)
{
}
}
//以下分析输出值,得到物理网卡
String rtValue = sb.substring(0);
int i = rtValue.indexOf("Physical Address. . . . . . . . . :");
while(i0)
{
rtValue = rtValue.substring(i + "Physical Address. . . . . . . . . :".length());
address.add(rtValue.substring(0,18));
i = rtValue.indexOf("Physical Address. . . . . . . . . :");
}
return address;
}
public static void main(String[] args)
{
List address = ProcessBuilderShow.getPhysicalAddress();
for(String add:address)
{
System.out.printf("物理网卡地址:%s%n", add);
}
}
}
在Java程序中获取本机IP
import java.net.InetAddress;
import java.util.Enumeration;
import java.net.NetworkInterface;
import java.util.*;
public class ipdisplay {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String allipaddress;
ArrayList ar = new ArrayList();
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
Enumeration cardipaddress = ni.getInetAddresses();
InetAddress ip = (InetAddress) cardipaddress.nextElement();
if(!ip.getHostAddress().equalsIgnoreCase("127.0.0.1") )
{ ar.add(ni.getName()+":");
allipaddress=ip.getHostAddress();
while(cardipaddress.hasMoreElements())
{
ip = (InetAddress) cardipaddress.nextElement();
allipaddress=allipaddress+" , "+ip.getHostAddress();
}
ar.add(allipaddress);
}
else
continue;
}
for(int i=0;iar.size();)
{
System.out.println(ar.get(i++));
}
}
}
Java如何获取本地计算机的IP地址和主机名
可以使用 InetAddress.getLocalHost(),代码如下:
import java.net.*;
public class App {
public static void main(String[] args) throws UnknownHostException {
InetAddress local = InetAddress.getLocalHost();
System.out.println("计算机名:" + local.getHostName());
System.out.println("IP:" + local.getHostAddress());
}
}
java过去本机ip地址的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java ip地址、java过去本机ip地址的信息别忘了在本站进行查找喔。
发布于:2022-11-24,除非注明,否则均为
原创文章,转载请注明出处。