一、使用InetAddress獲取本機IP地址
Java提供了InetAddress類來獲取本機IP地址。通過調用getLocalHost方法可以獲取本機的InetAddress對象,然後可以通過getHostAddress方法獲取IP地址。
import java.net.InetAddress; public class GetIPAddress { public static void main(String[] args) throws Exception { InetAddress address = InetAddress.getLocalHost(); System.out.println("本機 IP 地址:" + address.getHostAddress()); } }
二、通過Socket獲取本機IP地址
通過Tcp Socket可以獲取本機IP地址。創建一個Tcp Socket對象後,調用其getLocalAddress方法即可獲取本機IP地址。
import java.net.Socket; public class GetIPAddress { public static void main(String[] args) throws Exception { Socket socket = new Socket(); String ip = socket.getLocalAddress().getHostAddress(); System.out.println("本機 IP 地址:" + ip); } }
三、使用NetworkInterface獲取本機IP地址
Java提供了NetworkInterface類來操作網路介面,可以通過NetworkInterface獲取本機IP地址。
import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class GetIPAddress { public static void main(String[] args) throws SocketException { Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); Enumeration inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (!inetAddress.isLinkLocalAddress() && !inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) { System.out.println("本機 IP 地址:" + inetAddress.getHostAddress()); } } } } }
四、使用HttpClient獲取外網IP地址
使用HttpClient發起網路請求,可以獲取外網IP地址。通過訪問帶有IP地址信息的網頁,可以獲取當前機器的外網IP地址。
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; public class GetIPAddress { public static void main(String[] args) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://pv.sohu.com/cityjson?ie=utf-8"); CloseableHttpResponse response = httpClient.execute(httpGet); String content = EntityUtils.toString(response.getEntity()); int start = content.indexOf("\"cip\":") + 7; int end = content.indexOf("\",\"cid\""); System.out.println("本機 IP 地址:" + content.substring(start, end)); } }
五、使用第三方API獲取外網IP地址
通過調用第三方API,可以獲取當前機器的外網IP地址。如:http://httpbin.org/ip、http://ip.taobao.com/service/getIpInfo.php,這些API返回的都是JSON格式數據。
import com.alibaba.fastjson.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class GetIPAddress { public static void main(String[] args) throws Exception { String urlStr = "http://httpbin.org/ip"; URL url = new URL(urlStr); URLConnection urlConnection = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String content = ""; String line; while ((line = in.readLine()) != null) { content += line; } JSONObject jsonObject = JSONObject.parseObject(content); System.out.println("本機 IP 地址:" + jsonObject.getString("origin")); } }
六、小結
以上是Java獲取當前IP地址的幾種方法,不同的場景可以使用不同的方法。如果只是需要獲取本機IP地址,可以使用 InetAddress 或 Socket;如果需要獲取外網IP地址,可以使用 HttpClient 或第三方 API。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/277434.html