ip java

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class helloworld {

	public static void main(String[] args) throws UnknownHostException {
		
		InetAddress ip = InetAddress.getByAddress(new byte[]{10,1,1,1});
		System.out.println(ip);
		
		ip = InetAddress.getByAddress("www.sdut.edu.cn", new byte[]{10,1,1,1});
		System.out.println(ip);
		
		ip = InetAddress.getByName("www.qq.com");
		System.out.println(ip);
		
		ip = InetAddress.getLocalHost();
		System.out.println(ip);
		
		System.out.println(ip.getHostName());
		System.out.println(Arrays.toString(ip.getAddress()));
	}

}

### 如何用Java获取公网IP地址 在 Java 中可以通过多种方式来实现获取公网 IP 地址的功能。以下是几种常见的方法: #### 方法一:通过 HTTP 请求第三方服务 可以利用一些提供公网 IP 查询的服务,例如 `httpbin.org` 或者其他类似的 API 接口。 ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class PublicIPAddress { public static void main(String[] args) { try { URL url = new URL("https://api.httpbin.org/ip"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } in.close(); System.out.println(content.toString()); } catch (Exception e) { e.printStackTrace(); } } } ``` 这种方法依赖于外部服务返回的数据结构,通常会以 JSON 格式返回公网 IP 地址[^1]。 --- #### 方法二:解析请求头中的客户端 IP 地址 如果是在 Web 应用程序中运行,则可以从 `HttpServletRequest` 对象中提取远程用户的 IP 地址。 ```java public static String getClientIpAddress(HttpServletRequest request) { String ip = request.getHeader("X-Forwarded-For"); // 可能经过代理 if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); // 客户端真实 IP } return ip; } ``` 此方法适用于服务器接收到的 HTTP 请求场景下,能够有效识别客户端的真实 IP 地址[^3]。 --- #### 方法三:本地网络配置检测 对于某些特定需求,也可以尝试直接读取本机网络接口的信息,但这仅限于局域网环境下的私有 IP 地址,并不适用公网 IP 的情况。 ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; public class LocalNetworkInfo { public static void main(String[] args) throws Exception { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface iface = networkInterfaces.nextElement(); Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); System.out.println(addr.getHostAddress()); } } } } ``` 需要注意的是,这种方式无法获得真实的外网 IP 地址,除非设备已经连接到支持 NAT 映射的家庭路由器或其他网关设备上[^2]。 --- ### 总结 上述三种方案各有优劣,在实际开发过程中可以根据具体业务场景选择合适的方式。如果是单纯为了知道自己的公网出口 IP,则推荐采用第一种基于互联网公开查询的方法;而当涉及到复杂的分布式架构设计时,则需综合考虑安全性等因素再决定最佳实践路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值