javaip地址,javaip地址化成整數

本文目錄一覽:

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

   Java中可以使用程序來獲取本地ip地址和mac地址,使用InetAddress這個工具類,示例如下:

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();}

 }

}

    也可以通過命令行窗口來查看本地ip和mac地址,輸入命令:ipconfig。

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地址

* 思想是訪問網站,得到返回的文本後解析出本機在外網的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);

}

/**

* 從外網提供者處獲得包含本機外網地址的字符串

* 從返回的字符串如下

* htmlheadtitleCurrent IP Check/title/headbodyCurrent 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(“”);

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

}

}

java 如何驗證ip地址

可以使用正則表達式驗證ip地址,ip地址分為v4和v6兩個版本,v4為32位,分4段,中間用.隔開,v6為128位,可分為4段32位中間用::隔開。

以下是驗證類詳細代碼:

import java.util.regex.Pattern;

/**

* A collection of utilities relating to InetAddresses.

*/

public class InetAddressUtils {

public static void main(String[] args){

String addr=”192.168.1.2″;

System.out.println(isIPv4Address(addr));

}

private static final Pattern IPV4_PATTERN =

Pattern.compile(

“^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$”);

private static final Pattern IPV6_STD_PATTERN =

Pattern.compile(

“^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$”);

private static final Pattern IPV6_HEX_COMPRESSED_PATTERN =

Pattern.compile(

“^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$”);

public static boolean isIPv4Address(final String input) {

return IPV4_PATTERN.matcher(input).matches();

}

public static boolean isIPv6StdAddress(final String input) {

return IPV6_STD_PATTERN.matcher(input).matches();

}

public static boolean isIPv6HexCompressedAddress(final String input) {

return IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches();

}

public static boolean isIPv6Address(final String input) {

return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input);

}

}

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 {

EnumerationNetworkInterface en = NetworkInterface.getNetworkInterfaces();

while (en.hasMoreElements()) {

NetworkInterface intf = (NetworkInterface) en.nextElement();

EnumerationInetAddress 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();

}

java如何獲取用戶真實的ip

1、如果服務器如果沒有採用反向代理,而且客戶端沒有用正向代理的話,那麼可以獲取客戶端的真實IP地址request.getRemoteAddr()

2、如果服務器如果沒有採用反向代理,而且客戶端有用正向代理的話,那麼通過request.getRemoteAddr()獲取客戶端的IP地址是客戶端 的代理服務器的地址,並不是客戶端的真實地址,

3、如果客戶端使用的是多層代理的話,服務器獲得的客戶端地址是客戶端的最外圍代理服務器的地址如果服務器如果採用反向代理服務器,不管客戶端採用的是何種方式訪問服務器。

//獲得客戶端真實IP地址的方法一:

public String getRemortIP(HttpServletRequest request) {  

    if (request.getHeader(“x-forwarded-for”) == null) {  

        return request.getRemoteAddr();  

    }  

    return request.getHeader(“x-forwarded-for”);  

}  

//獲得客戶端真實IP地址的方法二:

public String getIpAddr(HttpServletRequest request) {  

    String ip = request.getHeader(“x-forwarded-for”);  

    if(ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {  

        ip = request.getHeader(“Proxy-Client-IP”);  

    }  

    if(ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {  

        ip = request.getHeader(“WL-Proxy-Client-IP”);  

    }  

    if(ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {  

        ip = request.getRemoteAddr();  

    }  

    return ip;  

}

java怎麼獲取請求的ip

java獲取外網ip地址方法:

public class Main {

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

System.out.println(Main.getRealIp());

}

public static String getRealIp() throws SocketException {

String localip = null;// 本地IP,如果沒有配置外網IP則返回它

String netip = null;// 外網IP

EnumerationNetworkInterface netInterfaces =

NetworkInterface.getNetworkInterfaces();

InetAddress ip = null;

boolean finded = false;// 是否找到外網IP

while (netInterfaces.hasMoreElements() !finded) {

NetworkInterface ni = netInterfaces.nextElement();

EnumerationInetAddress address = ni.getInetAddresses();

while (address.hasMoreElements()) {

ip = address.nextElement();

if (!ip.isSiteLocalAddress()

!ip.isLoopbackAddress()

ip.getHostAddress().indexOf(“:”) == -1) {// 外網IP

netip = ip.getHostAddress();

finded = true;

break;

} else if (ip.isSiteLocalAddress()

!ip.isLoopbackAddress()

ip.getHostAddress().indexOf(“:”) == -1) {// 內網IP

localip = ip.getHostAddress();

}

}

}

if (netip != null !””.equals(netip)) {

return netip;

} else {

return localip;

}

}

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/241028.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:25
下一篇 2024-12-12 12:25

相關推薦

  • cmd看地址

    本文將從多個方面詳細闡述cmd看地址,包括如何查看本機IP地址、如何查看路由器IP、如何查看DNS服務器IP等等。 一、查看本機IP地址 要查看本機IP地址,首先需要打開cmd窗口…

    編程 2025-04-29
  • 如何輸入三個整數,並輸出最大值Python

    對於初學者來說,輸入三個整數並輸出它們的最大值可能是一個比較基礎的問題。然而,它卻包含了Python中許多基本知識點的應用,因此學習它可以讓我們更好地理解Python編程語言。 一…

    編程 2025-04-29
  • 尚硅谷官網地址用法介紹

    尚硅谷是國內一家領先的技術培訓機構,提供了眾多IT職業的培訓,包括Java、Python、大數據、前端、人工智能等方向。其官網地址為http://www.atguigu.com/。…

    編程 2025-04-29
  • Python隨機生成100內的10個整數

    本文將從以下幾個方面詳細闡述Python隨機生成100內的10個整數: 一、random庫介紹 在Python中,生成隨機數可以使用random庫。random庫包括兩種類型的函數…

    編程 2025-04-29
  • 全能編程開發工程師必備技能——如何優化大整數的計算

    本文將會為你分享如何解決大整數計算問題,以9999999967為例,我們將從多個方面對其做詳細闡述,並給出完整的代碼示例。 一、大整數的表示方法 在計算機中,我們通常採用二進制數來…

    編程 2025-04-29
  • 整數的因子包含自身嗎

    本篇文章將從數學概念的角度、常用算法的應用、程序實現的方法等多個方面,對整數的因子包含自身的問題進行詳細闡述。 一、質因數分解法 將整數進行質因數分解,若分解結果中所有質因子的指數…

    編程 2025-04-29
  • Python中的整數類型int類總覽

    本文將從多個方面,對Python中的整數類型int類進行全面介紹和闡述。 一、數據類型及基本操作 在Python中,整數類型的數據類型為int。在Python3.x中,整數類型的范…

    編程 2025-04-28
  • Python計算ab之間整數的和

    本篇文章將闡述如何用Python計算ab之間整數的和以及使用for循環求解,希望本文能對正在學習Python的人們有所幫助。 一、計算ab之間整數的和 首先我們需要明確什麼是ab之…

    編程 2025-04-28
  • Python元組元素分成單個整數

    本文將介紹如何將Python元組中的元素分成單個整數,並提供多種實現方式。 一、使用for循環遍曆元組實現 可以通過for循環遍曆元組的每一個元素,再將其轉換成整數,並存儲在新的列…

    編程 2025-04-28
  • Idea2022變更Git地址

    本文將從以下幾個方面對Idea2022變更Git地址進行詳細闡述: 一、GitHub上修改Git倉庫地址 1、登錄GitHub,找到需要修改的Git倉庫 2、在代碼頁面點擊右上角的…

    編程 2025-04-28

發表回復

登錄後才能評論