導航:首頁 > 編程語言 > 獲取本機ip地址java

獲取本機ip地址java

發布時間:2022-10-04 20:38:03

A. 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。

B. 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());

}
}

C. java怎麼獲取本地ip地址

Jva獲取本地的IP地址,這個你就需要到你的手機的網路客戶端裡面進行設置了,這個話都是可以去查看的。

D. java怎麼獲取本機的IP地址和MAC

Map<String, String> map = System.getenv();
String userName = map.get("USERNAME");// 獲取用戶名
String computerName = map.get("COMPUTERNAME");// 獲取計算機名
詳細請查看http://blog.csdn.net/zhangxu365/article/details/6883573
String userDomain = map.get("USERDOMAIN");// 獲取計算機域名

E. java獲得IP地址

簡單實現代碼如下:
js獲取來源頁地址方法:
var url = document.referrer;
document.write(url);
jsp獲取來源頁地址方法:
String url = request.getHeader(」Referer」);
System.out.println(url);
對比兩個方法:
1.js里是」referrer」,jsp里是」referer」,前者比後者多一個」r」;
2.前者如直接輸入網址,則顯示為空,後者顯示null;
import java.net.*;

public class ip5 {

public static void main(String args[]) throws Exception {

String ip = InetAddress.getLocalHost().getHostAddress();

System.out.println(ip);

}

}

F. java如何查詢本機ip地址和mac地址

//獲取mac地址
(){
try{
Enumeration<NetworkInterface>allNetInterfaces=NetworkInterface.getNetworkInterfaces();
byte[]mac=null;
while(allNetInterfaces.hasMoreElements()){
NetworkInterfacenetInterface=(NetworkInterface)allNetInterfaces.nextElement();
if(netInterface.isLoopback()||netInterface.isVirtual()||!netInterface.isUp()){
continue;
}else{
mac=netInterface.getHardwareAddress();
if(mac!=null){
StringBuildersb=newStringBuilder();
for(inti=0;i<mac.length;i++){
sb.append(String.format("%02X%s",mac[i],(i<mac.length-1)?"-":""));
}
if(sb.length()>0){
returnsb.toString();
}
}
}
}
}catch(Exceptione){
_logger.error("MAC地址獲取失敗",e);
}
return"";
}

//獲取ip地址
(){
try{
Enumeration<NetworkInterface>allNetInterfaces=NetworkInterface.getNetworkInterfaces();
InetAddressip=null;
while(allNetInterfaces.hasMoreElements()){
NetworkInterfacenetInterface=(NetworkInterface)allNetInterfaces.nextElement();
if(netInterface.isLoopback()||netInterface.isVirtual()||!netInterface.isUp()){
continue;
}else{
Enumeration<InetAddress>addresses=netInterface.getInetAddresses();
while(addresses.hasMoreElements()){
ip=addresses.nextElement();
if(ip!=null&&ipinstanceofInet4Address){
returnip.getHostAddress();
}
}
}
}
}catch(Exceptione){
_logger.error("IP地址獲取失敗",e);
}
return"";
}

希望能幫助到你

G. 如何用 Java 獲取系統 IP

importjava.net.*;

publicclassTest6{

publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
InetAddressia=null;
try{
ia=ia.getLocalHost();

Stringlocalname=ia.getHostName();
Stringlocalip=ia.getHostAddress();
System.out.println("本機名稱是:"+localname);
System.out.println("本機的ip是:"+localip);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

}

閱讀全文

與獲取本機ip地址java相關的資料

熱點內容
怎麼解綁app的支付寶賬號 瀏覽:909
ip地址伺服器不可用怎麼解決方法 瀏覽:181
為什麼軟體需要伺服器 瀏覽:61
redis操作命令大全 瀏覽:597
python字元串重復索引 瀏覽:959
為什麼香信新版本連接不上伺服器 瀏覽:48
元旦程序員打羽毛球 瀏覽:612
otc焊接機器人離線編程教學 瀏覽:410
51單片機的ea引腳有何用途 瀏覽:205
centos查看用戶命令 瀏覽:838
程序員臉胖 瀏覽:744
hdfs在主目錄下創建文件夾 瀏覽:798
股票選股器源碼公式如何弄 瀏覽:31
伺服器如何使用在微信上 瀏覽:326
app登不了是怎麼回事 瀏覽:252
dd命令u盤 瀏覽:568
單片機生日快樂程序 瀏覽:891
安卓手機連車載的叫什麼 瀏覽:223
怎麼讓自己的手機鍵盤變得好看app 瀏覽:53
能看qq的文件夾 瀏覽:515