導航:首頁 > 編程語言 > java獲得本機外網的ip地址

java獲得本機外網的ip地址

發布時間:2024-12-28 03:12:22

『壹』 java中如何獲取到本機的外網ip地址

java獲取本機的外網ip示例:
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 獲取本機外網IP地址
* 思想是訪問網站http://checkip.dyndns.org/,得到返回的文本後解析出本機在外網的IP地址
* @author pieryon
*
*/
public class ExternalIpAddressFetcher {
// 外網IP提供者的網址
private String externalIpProviderUrl;

// 本機外網IP地址
private String myExternalIpAddress;

public ExternalIpAddressFetcher(String externalIpProviderUrl) {
this.externalIpProviderUrl = externalIpProviderUrl;

String returnedhtml = fetchExternalIpProviderHTML(externalIpProviderUrl);

parse(returnedhtml);
}

/**
* 從外網提供者處獲得包含本機外網地址的字元串
* 從http://checkip.dyndns.org返回的字元串如下
* <html><head><title>Current IP Check</title></head><body>Current IP Address: 123.147.226.222</body></html>
* @param externalIpProviderUrl
* @return
*/
private String fetchExternalIpProviderHTML(String externalIpProviderUrl) {
// 輸入流
InputStream in = null;

// 到外網提供者的Http連接
HttpURLConnection httpConn = null;

try {
// 打開連接
URL url = new URL(externalIpProviderUrl);
httpConn = (HttpURLConnection) url.openConnection();

// 連接設置
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");

// 獲取連接的輸入流
in = httpConn.getInputStream();
byte[] bytes=new byte[1024];// 此大小可根據實際情況調整

// 讀取到數組中
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}

// 將位元組轉化為為UTF-8的字元串
String receivedString=new String(bytes,"UTF-8");

// 返回
return receivedString;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}

// 出現異常則返回空
return null;
}

/**
* 使用正則表達式解析返回的HTML文本,得到本機外網地址
* @param html
*/
private void parse(String html){
Pattern pattern=Pattern.compile("(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})", Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(html);
while(matcher.find()){
myExternalIpAddress=matcher.group(0);
}
}

/**
* 得到本機外網地址,得不到則為空
* @return
*/
public String getMyExternalIpAddress() {
return myExternalIpAddress;
}

public static void main(String[] args){
ExternalIpAddressFetcher fetcher=new ExternalIpAddressFetcher("http://checkip.dyndns.org/");

System.out.println(fetcher.getMyExternalIpAddress());
}
}

『貳』 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.*;

public class Test {

public static void main(String[] args) throws UnknownHostException {
String IP = null;
String host = null;
InetAddress ia;

ia = InetAddress.getLocalHost();
host = ia.getHostName();// 獲取計算機名字
IP = ia.getHostAddress();// 獲取IP

System.out.println(host);
System.out.println(IP);
}

}

『肆』 JAVA獲取外網IP

請參考以下代碼,可以獲得任意給定網卡的ip地址:

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class InternetTest {

public static void main(String[] args) {
String netCard = "lo";
try {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
if (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
if (netCard.equals(netInterface.getName())) {
// 子介面,linux下會取到父介面??
Enumeration<NetworkInterface> subnetInterfaces = netInterface
.getSubInterfaces();
while (subnetInterfaces.hasMoreElements()) {
NetworkInterface subnetInterface = subnetInterfaces
.nextElement();
System.out.println(subnetInterface.getName());
Enumeration<InetAddress> subaddresses = netInterface
.getInetAddresses();
while (subaddresses.hasMoreElements()) {
InetAddress subaddress = subaddresses.nextElement();
System.out.println(subaddress.getHostAddress());
}
}
// 列印介面下所有IP
System.out.println(netInterface.getName());
Enumeration<InetAddress> addresses = netInterface
.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
System.out.println(address.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}


閱讀全文

與java獲得本機外網的ip地址相關的資料

熱點內容
紅底白色的心是什麼app的標志 瀏覽:163
小冤家APP角色怎麼變回家長 瀏覽:822
夢幻西遊合寵模擬器網站是什麼 瀏覽:420
諾基亞930最新版本 瀏覽:201
ps製作主kv文件過大 瀏覽:884
車端面如何編程 瀏覽:279
win10u盤備份時間長 瀏覽:617
文件夾怎麼轉換為pdf 瀏覽:502
2008打開登錄密碼忘記了 瀏覽:771
蘋果7如何授權應用程序 瀏覽:899
怎樣把舊的文檔保存到桌面文件夾 瀏覽:827
wps雲數據如何恢復 瀏覽:496
微信發送過來文件 瀏覽:300
怎麼改合同網站 瀏覽:73
網路鬥地主記牌器怎麼實現的 瀏覽:377
ps鏡像文件製作教程 瀏覽:45
系統分頁文件大小設置多少 瀏覽:447
win10有線無法上網 瀏覽:339
wps無法訪問指定文件 瀏覽:96
iphone4震動壞了 瀏覽:217

友情鏈接