java獲取本機地址
㈠ java 如何獲得本機ip
Enumeration<NetworkInterface> netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
System.out.println("DisplayName:" + ni.getDisplayName());
System.out.println("Name:" + ni.getName());
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
System.out.println("IP:"
+ ips.nextElement().getHostAddress());
}
}
} catch (Exception e) {
e.printStackTrace();
}
Enumeration<NetworkInterface> netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
System.out.println("DisplayName:" + ni.getDisplayName());
System.out.println("Name:" + ni.getName());
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
System.out.println("IP:"
+ ips.nextElement().getHostAddress());
}
}
} catch (Exception e) {
e.printStackTrace();
}
看看上面的代碼,能否幫到你?
㈡ java通過域名怎麼獲取本機ip
代碼親測可用:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
InetAddress myIpAddress = null;
InetAddress[] myServer = null;
public static void main(String args[]) {
TestInetAddress address = new TestInetAddress();
System.out.println("Your host IP is: " + address.getLocalhostIP());
String domain = "www.163.com";
System.out.println("The server domain name is: " + domain);
InetAddress[] array = address.getServerIP(domain);
int count=0;
for(int i=1; i<array.length; i++){
System.out.println("ip "+ i +" "+ address.getServerIP(domain)[i-1]);
count++;
}
System.out.println("IP address total: "+count);
}
/**
* 獲得 localhost 的IP地址
* @return
*/
public InetAddress getLocalhostIP() {
try {
myIpAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return (myIpAddress);
}
/**
* 獲得某域名的IP地址
* @param domain 域名
* @return
*/
public InetAddress[] getServerIP(String domain) {
try {
myServer = InetAddress.getAllByName(domain);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return (myServer);
}
}
㈢ 如何利用JAVA獲得本機實際IP
import <a href="https://www..com/s?wd=java.net&tn=44039180_cpr&fenlei=-4Bmy-bIi4WUvYETgN-TLwGUv3En1c3nW6vPWDz" target="_blank" class="-highlight">java.net</a>.*;
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InetAddress ia=null;
try {
ia=ia.getLocalHost();
String localname=ia.getHostName();
String localip=ia.getHostAddress();
System.out.println("本機名稱是:"+ localname);
System.out.println("本機的ip是 :"+localip);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
㈣ java 獲取本機Ip地址和對應的mac地址
import java.net.*;
public class NetInfo {
public static void main(String[] args) {
new NetInfo().say();
}
public void say() {
try {
InetAddress i = InetAddress.getLocalHost();
System.out.println(i); //計算機名稱和ip
System.out.println(i.getHostName()); //名稱
System.out.println(i.getHostAddress()); //只獲得ip
}
catch(Exception e){e.printStackTrace();}
}
}
㈤ Java如何獲取本地計算機的IP地址和主機名
可以使用 InetAddress.getLocalHost(),代碼如下:
importjava.net.*;
publicclassApp{
publicstaticvoidmain(String[]args)throwsUnknownHostException{
InetAddresslocal=InetAddress.getLocalHost();
System.out.println("計算機名:"+local.getHostName());
System.out.println("IP:"+local.getHostAddress());
}
}
㈥ java如何查詢本機ip地址和mac地址
Java中可以使用程序來獲取本地ip地址和mac地址,使用InetAddress這個工具類,示例如下:
importjava.net.*;
publicclassNetInfo{
publicstaticvoidmain(String[]args){
newNetInfo().say();
}
publicvoidsay(){
try{
InetAddressi=InetAddress.getLocalHost();
System.out.println(i);//計算機名稱和IP
System.out.println(i.getHostName());//名稱
System.out.println(i.getHostAddress());//只獲得IP
}
catch(Exceptione){e.printStackTrace();}
}
}
也可以通過命令行窗口來查看本地ip和mac地址,輸入命令:ipconfig。
㈦ 怎樣用JAVA程序獲取本機ip
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test
{
public static void main(String[] args) throws UnknownHostException
{
InetAddress inet = InetAddress.getLocalHost();
System.out.println("本機的ip=" + inet.getHostAddress());
}
}