當前位置:首頁 » 編程語言 » java本機ip

java本機ip

發布時間: 2023-12-12 02:41:30

⑴ 我在本機上,想寫一個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地址

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地址 網關地址 子網

提供一種可行的方法。供你參考。思路是利用操作系統的shell,執行相應的命令。

以下以WINDOW操作系統為例。LINUX的思路相同。

1,在E;下建立如下兩個bat文件,內容分別如下:

e:setip1.bat文件內容:

rem設置IP、子網掩碼、默認網關

c:

cd

netshexece:setip.bat

另一個文件e:setip.bat文件內容:

interface

ip

setaddress"本地連接"static192.168.1.111255.255.255.0192.168.1.1

exit

2,執行腳本命令的JAVA程序

⑸ java中獲取本地IP地址

方法如下:
方法一,使用CMD命令:
public static String getLocalIPForCMD(){
StringBuilder sb = new StringBuilder();
String command = "cmd.exe /c ipconfig | findstr IPv4";
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = br.readLine()) != null){
line = line.substring(line.lastIndexOf(":")+2,line.length());
sb.append(line);
}
br.close();
p.destroy();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
方法二,使用Java方法:
public static String getLocalIPForJava(){
StringBuilder sb = new StringBuilder();
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
&& inetAddress.isSiteLocalAddress()) {
sb.append(inetAddress.getHostAddress().toString()+"\n");
}
}
}
} catch (SocketException e) { }
return sb.toString();
}

熱點內容
cvr網路存儲 發布:2025-01-24 17:24:52 瀏覽:415
腿套壓縮襪 發布:2025-01-24 17:05:16 瀏覽:458
電腦如何將安卓軟體卸載干凈 發布:2025-01-24 17:03:06 瀏覽:489
hello密碼怎麼破解 發布:2025-01-24 17:03:06 瀏覽:73
pspfifa無緩存 發布:2025-01-24 16:45:13 瀏覽:165
androidhandler機制 發布:2025-01-24 16:41:10 瀏覽:936
安卓系統如何下載aov 發布:2025-01-24 16:29:53 瀏覽:573
iptables允許ip訪問 發布:2025-01-24 16:19:58 瀏覽:932
安卓80如何識別存儲卡許可權 發布:2025-01-24 16:19:54 瀏覽:232
存儲介質價格 發布:2025-01-24 16:19:18 瀏覽:151