for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLinkLocalAddress() && !inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress(); } } }无法获取wifi环境下的真实ip
时间: 2024-02-14 22:21:40 浏览: 29
这段代码是获取本地网络接口的IP地址,但是在 WiFi 环境下,设备是通过路由器分配的内网地址进行连接的,无法直接获取到公网 IP 地址。如果需要获取公网 IP 地址,可以使用第三方服务提供商的 API 接口。另外需要注意的是,一些网络环境下可能会有多个公网 IP 地址,需要根据实际情况选择正确的 IP 地址。
相关问题
怎么通过 Enumeration<URL> resources = classLoader.getResources(path); 获取jar内的文件
通过 `classLoader.getResources(path)` 获取到的是一个URL列表,其中包含了符合指定路径的所有资源文件的URL,包括位于jar包中的资源文件的URL。我们可以通过遍历这个URL列表,获取到每个资源文件的URL,然后使用Java的JarURLConnection类来读取jar包中的资源文件。
下面是一个示例代码:
```java
ClassLoader classLoader = getClass().getClassLoader();
Enumeration<URL> resources = classLoader.getResources("com/example/config.properties");
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
if (url.getProtocol().equals("file")) {
// 处理普通文件
File file = new File(url.toURI());
// 读取文件内容
} else if (url.getProtocol().equals("jar")) {
// 处理jar包中的文件
JarURLConnection connection = (JarURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
// 读取文件内容
}
}
```
以上代码中,我们首先通过 `classLoader.getResources(path)` 获取到了符合指定路径的所有资源文件的URL,然后通过 `hasMoreElements()` 和 `nextElement()` 方法遍历这个URL列表,对于每个URL,判断它的协议是 `file` 还是 `jar`。如果是 `file` 协议,说明该资源文件是一个普通文件,我们可以使用 `File` 类获取到该文件的路径,然后读取文件内容即可。如果是 `jar` 协议,说明该资源文件位于jar包中,我们可以通过 `JarURLConnection` 类获取到该文件的输入流,然后读取文件内容即可。需要注意的是,读取文件时,要根据文件的类型进行不同的处理。
public static List<String> getSerialPortList() { List<String> systemPorts = new ArrayList<>(); //获得系统可用的端口 Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { String portName = portList.nextElement().getName();//获得端口的名字 systemPorts.add(portName); } return systemPorts; }
您的第一个问题是关于获取可用串口列表的方法。上述代码是一个静态方法,它返回一个包含系统可用串口名称的字符串列表。下面是对代码的解释:
1. 创建一个空的字符串列表 `systemPorts`,用于存储系统可用串口的名称。
2. 使用 `CommPortIdentifier.getPortIdentifiers()` 方法获取系统可用串口的枚举。
3. 使用 `while` 循环遍历枚举中的每个串口。
4. 在循环中,使用 `getName()` 方法获取串口的名称,并将其添加到 `systemPorts` 列表中。
5. 循环结束后,返回 `systemPorts` 列表。
这段代码使用了 Java 的 `CommPortIdentifier` 类来获取系统可用串口。请注意,您需要在代码中导入正确的包,以便使用 `CommPortIdentifier` 类。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)