java 获取ip的网络号,网络 - 在J中获取“外部”IP地址

网络 - 在J中获取“外部”IP地址

我不太确定如何获取机器的外部IP地址,因为网络外的计算机会看到它。

我的以下IPAddress类仅获取计算机的本地IP地址。

public class IPAddress {

private InetAddress thisIp;

private String thisIpAddress;

private void setIpAdd() {

try {

InetAddress thisIp = InetAddress.getLocalHost();

thisIpAddress = thisIp.getHostAddress().toString();

} catch (Exception e) {

}

}

protected String getIpAddress() {

setIpAdd();

return thisIpAddress;

}

}

11个解决方案

149 votes

我不确定您是否可以从本地计算机上运行的代码中获取该IP。

但是,您可以构建在网站上运行的代码,例如在JSP中运行,然后使用返回请求来源的IP的内容:

request.getRemoteAddr()

或者只是使用已经存在的服务来执行此操作,然后解析服务中的答案以找出IP。

使用AWS等Web服务

import java.net.*;

import java.io.*;

URL whatismyip = new URL("http://checkip.amazonaws.com");

BufferedReader in = new BufferedReader(new InputStreamReader(

whatismyip.openStream()));

String ip = in.readLine(); //you get the IP as a String

System.out.println(ip);

bakkal answered 2019-04-26T09:30:19Z

70 votes

@stivlo的评论之一值得回答:

您可以使用亚马逊服务[http://checkip.amazonaws.com]

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URL;

public class IpChecker {

public static String getIp() throws Exception {

URL whatismyip = new URL("http://checkip.amazonaws.com");

BufferedReader in = null;

try {

in = new BufferedReader(new InputStreamReader(

whatismyip.openStream()));

String ip = in.readLine();

return ip;

} finally {

if (in != null) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

Will answered 2019-04-26T09:30:51Z

15 votes

事实是,“你不能”,因为你提出了这个问题。 NAT发生在协议之外。 您的计算机内核无法知道您的NAT盒如何从外部IP地址映射到内部IP地址。 这里的其他答案提供了涉及与外部网站交谈的方法的技巧。

bmargulies answered 2019-04-26T09:31:16Z

9 votes

正如@Donal Fellows所写,你必须查询网络接口而不是机器。 来自javadocs的代码对我有用:

以下示例程序列出了计算机上的所有网络接口及其地址:

import java.io.*;

import java.net.*;

import java.util.*;

import static java.lang.System.out;

public class ListNets {

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

Enumeration nets = NetworkInterface.getNetworkInterfaces();

for (NetworkInterface netint : Collections.list(nets))

displayInterfaceInformation(netint);

}

static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {

out.printf("Display name: %s\n", netint.getDisplayName());

out.printf("Name: %s\n", netint.getName());

Enumeration inetAddresses = netint.getInetAddresses();

for (InetAddress inetAddress : Collections.list(inetAddresses)) {

out.printf("InetAddress: %s\n", inetAddress);

}

out.printf("\n");

}

}

以下是示例程序的示例输出:

Display name: TCP Loopback interface

Name: lo

InetAddress: /127.0.0.1

Display name: Wireless Network Connection

Name: eth0

InetAddress: /192.0.2.0

来自docs.oracle.com

domih answered 2019-04-26T09:32:01Z

9 votes

所有这些仍然运行顺利! (截至2016年12月19日)

[http://checkip.amazonaws.com/]

[http://icanhazip.com/]

[http://www.trackip.net/ip]

[http://myexternalip.com/raw]

[http://ipecho.net/plain]

[http://curlmyip.com/](2016年12月17日)

[http://bot.whatismyipaddress.com]

建议:不要直接依赖其中一个; 尝试使用一个,但有一个考虑他人的应急计划! 你使用的越多越好!

祝好运!

FranciscoBouza answered 2019-04-26T09:33:23Z

5 votes

对像www.whatismyip.com这样的网站进行HttpURLConnection并解析:-)

nc3b answered 2019-04-26T09:33:48Z

4 votes

[http://jstun.javawi.de/]会这样做 - 只要您的网关设备执行STUN)大多数都会这样做)

user461467 answered 2019-04-26T09:34:13Z

4 votes

这个怎么样? 它很简单,对我来说效果最好:)

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

public class IP {

public static void main(String args[]) {

new IP();

}

public IP() {

URL ipAdress;

try {

ipAdress = new URL("http://myexternalip.com/raw");

BufferedReader in = new BufferedReader(new InputStreamReader(ipAdress.openStream()));

String ip = in.readLine();

System.out.println(ip);

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

Sparkoenig answered 2019-04-26T09:34:38Z

1 votes

这并不容易,因为局域网内的机器通常不关心其路由器的外部IP到互联网......它根本不需要它!

我建议你通过打开像[http://www.whatismyip.com/]这样的网站并通过解析html结果来获取IP号码来利用它。它应该不那么难!

Jack answered 2019-04-26T09:35:09Z

0 votes

如果您正在使用基于JAVA的webapp,并且想要获取客户端(通过浏览器发出请求的人),则外部ip尝试在公共域中部署应用程序并使用request.getRemoteAddr()来读取外部IP地址。

Jay answered 2019-04-26T09:35:34Z

0 votes

System.out.println(pageCrawling.getHtmlFromURL("http://ipecho.net/plain"));

Mohsen Abasi answered 2019-04-26T09:35:52Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以通过使用Application类或者Java动态链接库的方式来获取IP地址。在Java,可以使用Application类的方法来获取本机IP地址,但是需要注意这个类只能在本机上使用,如果要访问外部的主机,还需要使用其他的方法。可以在主程序一个java.util.Application()方法,然后在子类定义一个Application类,通过这个类可以获取到本机IP地址。另外,也可以通过Java动态链接库的方式来获取IP地址,可以将需要的IP地址添加到动态链接库,然后在需要用到时直接调用即可。还可以通过javax.dll的API或者JDK的API来获取IP地址,但这些方法相对较少使用。另外,还可以通过java.io的API来获取IP地址,但这种方法相对复杂一些,需要先安装javaio库。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [java获取本机ip的方法](https://blog.csdn.net/qq_42751978/article/details/130167416)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [java获取登陆用户ip方法](https://blog.csdn.net/qq_42751978/article/details/130584308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值