-
2020-04-28 11:54:37
原文地址
https://blog.csdn.net/wu_cai_/article/details/52831455
获取本地IP地址的方法
public static String getLocalAddress(){ String ip = ""; try { ip = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ip; }
获取外网本机的IP地址的方法
一种可行的获取方法,是通过http请求从网站中取出ip字段。例如:http://ip.chinaz.com/
通过这个网址,加上简单的正则表达式,即可得到。
public static String getV4IP(){ String ip = ""; String chinaz = "http://ip.chinaz.com"; StringBuilder inputLine = new StringBuilder(); String read = ""; URL url = null; HttpURLConnection urlConnection = null; BufferedReader in = null; try { url = new URL(chinaz); urlConnection = (HttpURLConnection) url.openConnection(); in = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(),"UTF-8")); while((read=in.readLine())!=null){ inputLine.append(read+"\r\n"); } //System.out.println(inputLine.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(in!=null){ try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>"); Matcher m = p.matcher(inputLine.toString()); if(m.find()){ String ipstr = m.group(1); ip = ipstr; //System.out.println(ipstr); } return ip; }
参考博客: http://blog.csdn.net/dugucanjian/article/details/47378063
更多相关内容 -
java获取本机外网ip
2015-08-13 20:23:06通过url链接到一个网址,该网址返回自己的ip地址(通过代理服务器访问) -
java获取本机的外网IP地址
2022-02-16 15:25:58百度找java获取本机的外网IP地址都是以前的垃圾代码,别人页面都改了,怎么拿得到。 之前学过python的爬虫,拿外网ip简单就是通过 https://ip.chinaz.com/这个网站去得到。 百度习惯了,md !还百度了半天。 ...百度找java获取本机的外网IP地址都是以前的垃圾代码,别人页面都改了,怎么拿得到。
之前学过python的爬虫,拿外网ip简单就是通过 https://ip.chinaz.com/这个网站去得到。
百度习惯了,md !还百度了半天。
//代码拿去,有一说一效率有点低 /** * 得到本机的外网的外网IP地址 作为该局域网的区分标志 * @return */ public static String getIp() { Document document = null; try { document = Jsoup.connect("https://ip.chinaz.com/").get(); } catch (IOException e) { e.printStackTrace(); } Elements select = document.select("#leftinfo > div.IcpMain02.bor-t1s02 > div.WhoIpWrap.jspu > div.WhwtdWrap.bor-b1s.col-gray03 > span:nth-child(1)"); String s = select.toString(); String ip = s.substring(s.indexOf(">") + 1, s.lastIndexOf("<")); return ip; }
//另一种方法,找到的有效: public static String getIP() { String ip = "http://pv.sohu.com/cityjson?ie=utf-8"; String inputLine = ""; String read = ""; String toIp=""; try { URL url = new URL(ip); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); while ((read = in.readLine()) != null) { inputLine += read; } String ObjJson=inputLine.substring(inputLine.indexOf("=")+1,inputLine.length()-1); // System.out.println(ObjJson); JSONObject jsonObj= JSON.parseObject(ObjJson); toIp=jsonObj.getString("cip"); // throw new Exception(); } catch (Exception e) { toIp=""; } return toIp; } /** * 根据域名获取ip地址 */ public static String getIfIP() { String toIp=""; try{ String pathName="baidu.com";//这里没有进行支持https协议的,可以自行进行切割字符串 InetAddress address =InetAddress.getAllByName(pathName)[0]; toIp=address.getHostAddress(); }catch (Exception e){ toIp=""; } return toIp; }
-
Java 获取本机的外网 IP
2021-09-27 11:39:34通过 HTTP 访问第三方获取 IP 的服务接口获取本机的外网 IP,例如: http://checkip.amazonaws.com/ https://ipv4.icanhazip.com/ http://bot.whatismyipaddress.com/ 等等… 考虑到这些第三方接口不一定 100% ...原理
通过 HTTP 访问第三方获取 IP 的服务接口获取本机的外网 IP,例如:
考虑到这些第三方接口不一定 100% 稳定,例如可能出现下线、错误、访问超时或太慢的情况,所以不能仅依赖其中之一。
下面提供一种方案,并发访问这些服务接口,并返回第一个成功的结果,也就是其中最快的一个返回结果。
实现
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.concurrent.*; import java.util.regex.Pattern; public class ExternalIPUtil { /** * IP 地址校验的正则表达式 */ private static final Pattern IPV4_PATTERN = Pattern.compile("^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"); /** * 获取 IP 地址的服务列表 */ private static final String[] IPV4_SERVICES = { "http://checkip.amazonaws.com/", "https://ipv4.icanhazip.com/", "http://bot.whatismyipaddress.com/" // and so on ... }; public static String get() throws ExecutionException, InterruptedException { List<Callable<String>> callables = new ArrayList<>(); for (String ipService : IPV4_SERVICES) { callables.add(() -> get(ipService)); } ExecutorService executorService = Executors.newCachedThreadPool(); try { // 返回第一个成功获取的 IP return executorService.invokeAny(callables); } finally { executorService.shutdown(); } } private static String get(String url) throws IOException { try (BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openStream()))) { String ip = in.readLine(); if (IPV4_PATTERN.matcher(ip).matches()) { return ip; } else { throw new IOException("invalid IPv4 address: " + ip); } } } }
线程池的
ExecutorService.invokeAny(callables)
方法用于并发执行多个线程,并拿到最快的执行成功的线程的返回值,只要有一个执行成功,其他失败的任务都会忽略。但是如果全部失败,例如本机根本就没有连外网,那么就会抛出ExecutionException
异常。Gist
https://gist.github.com/wucao/811e0bbe08f2dfab527a065d052d12a5
关注我
-
java获取本机的外网IP地址(亲测有效)
2020-04-13 01:46:17获取本机的外网地址 如果下面正确,请留下您宝贵的赞 package untils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.*; import java.util...获取本机的外网地址
如果下面正确,请留下您宝贵的赞
package untils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.*; import java.util.Enumeration; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author CBeann * @create 2020-04-13 1:31 */ public class IPUntils { public static void main(String[] args) throws Exception { System.out.println(IPUntils.getInterIP1()); System.out.println(IPUntils.getInterIP2()); System.out.println(IPUntils.getOutIPV4()); } public static String getInterIP1() throws Exception { return InetAddress.getLocalHost().getHostAddress(); } public static String getInterIP2() throws SocketException { String localip = null;// 本地IP,如果没有配置外网IP则返回它 String netip = null;// 外网IP Enumeration<NetworkInterface> netInterfaces; netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded = false;// 是否找到外网IP while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> 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; } } public static String getOutIPV4() { String ip = ""; String chinaz = "http://ip.chinaz.com"; StringBuilder inputLine = new StringBuilder(); String read = ""; URL url = null; HttpURLConnection urlConnection = null; BufferedReader in = null; try { url = new URL(chinaz); urlConnection = (HttpURLConnection) url.openConnection(); in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8")); while ((read = in.readLine()) != null) { inputLine.append(read + "\r\n"); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>"); Matcher m = p.matcher(inputLine.toString()); if (m.find()) { String ipstr = m.group(1); ip = ipstr; } return ip; } }
-
使用java获取本机外网IP
2021-02-08 16:20:41使用java获取本机外网IP地址及原理 原理: 随便百度一个<外网ip地址查询>就可以查询出自己的IP地址 其实就是将页面上内容下载到本地即可。 例如: 点开后可以看到 就是将这个网页的内容下载... -
java获取本机外网IP地址
2013-06-28 14:05:41package ip.test; 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; -
java代码得到外网ip地址
2012-11-22 17:30:53java代码得到外网ip地址,java调net webservice远程联调用得着 -
java获取linux服务器上的IP操作
2020-08-24 20:40:35主要介绍了java获取linux服务器上的IP操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
Java获取(外网)网络IP和本机真实IP
2022-01-04 10:59:41/** * IP地址相关工具类 */ public class IpUtil { private static final Logger logger = LoggerFactory....获取本机的外网ip地址</h2> * @return */ public static String getV4OrV6IP() { String ip = -
Java获取电脑外网ip地址方法
2018-07-16 16:49:23废话不多说,直接上代码 /** 接口超时时间 */ private static final Integer TIME_OUT = 1000; public static String INTRANET_IP = getIntranetIp();... public static String INTERNET_IP = getV4IP(); // ... -
Java 获取本机IP地址
2021-04-30 09:50:50在Java中如何准确的获取到本机IP地址呢?网上大部分的做法是InetAddress.getLocalHost().getHostAddress()。这的确能获取到本机IP地址,但是是不准确的。因为忽略了一个问题,网络环境是多变的,一台计算机不同的... -
linux命令获取本机外网公网ip地址,java程序获取本机外网公网ip地址 代码
2019-11-07 19:46:22linux命令获取本机外网ip地址 1 直接返回ip 最快 稳定 curl ns1.dnspod.net:6666 curl whatismyip.akamai.com curl icanhazip.com curl members.3322.org/dyndns/getip 慢 或 不稳定(有时无响应) curl ipecho.net/... -
Windows系统下Java程序获得本机IP地址
2021-01-18 21:12:03最近完成一个实验,需要程序自动得到本机的IP地址。 Java.net中提供了InetAddress.getLocalHost()方法,来得到IP地址,但是由于主机网卡的复杂性,往往存在一部主机对应多个网卡的情况,此时getLocalHost()方法得到... -
java获取本机的外网ip
2017-02-15 10:00:24原文:java获取本机的外网ip 源代码下载地址:http://www.zuidaima.com/share/1901820081638400.htm public static String getWebIp() { try { URL url = new URL(... -
Java获取本机公网IP方法汇总
2019-12-27 11:39:02原理:通过类似ipip.net站长工具,获取访问者设置IP信息,由于国内设置多级路由跳转,通过该方法获取的IP地址准确性十分的高(毕竟别人就是做这个事的。。)缺陷就是官方未提供外部接口,只能通过正则表达式对页面上... -
Java 获取本机的外网ip
2015-09-24 10:44:19Java 获取本机的外网ip. 本机为linux系统 快解救一下我吧 -
Java获取外网IP地址,常用于用户访问路径分析
2019-02-22 07:39:07获取外网IP /** * 获取外网IP * @param request * @return */ public static String getIpAddr(HttpServletRequest request) { String ipAddress = null; // ipAddress = this.getRequest().getRemoteAddr... -
java获取本机ip和本机公网ip
2022-03-03 14:54:43本机ip public static String getIpAddr(HttpServletRequest request) { if (request == null) { return null; } String ip = null; // X-Forwarded-For:Squid 服务代理 String ipAddresses = request.... -
java获取Centos7服务器网卡ip 子网掩码 默认网关 DNS 同时设置网卡 及 重启网卡参考
2019-04-01 16:04:44java获取Centos7服务器网卡ip 子网掩码 默认网关 DNS 同时设置网卡 及 重启网卡参考 -
java获取外网IP和省市区,抓取方式
2015-12-03 11:39:28你遇到过本地IP192.168.0.1但你想取到自己的外网IP或自己的省市区吗?这些统统都不是事,是事也就烦一会,下载下来执行main方法便可获取到位置和IP。带解析JAR包,最权威最给力的最最最最权威的方法。市面上绝对没有... -
Java获取外网ip地址
2015-08-09 17:48:22最近在做一个app,其中有一个功能点是获取本机的外网ip,网上流传的绝大部分都是获取局域网ip的方法,有些似乎能够获取外网ip方法但也已经失效。 大部分提到的都是 ip = InetAddress.getLocalHost().... -
java代码 获取到本机的真实网卡IP与所有的IPV4IP地址
2020-06-07 11:39:53在网上找到了很多关于这方面的资料,发现他们的方式只能获取到192.168.*.*这个 环回地址,还有一些方式获取到的是本机中所有网卡的ip,包括 虚拟机网卡,蓝牙虚拟网卡,这个和与访问外网的网卡比起来,只差一个过滤... -
Java获取本机IP地址(对外的IP地址和局域网的IP地址)
2017-06-25 17:43:40获取对外的IP地址,获取本机在局域网中的IP地址 -
Java获取本机公网ip
2016-06-27 14:07:47import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; /** * * @anthor leo * @date 2016年6月27日下午12:40:14 * @description ... -
java 获取本机内网ip、外网ip
2019-11-12 18:17:13```java import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import org.apache.commons.lang3.StringUtils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.n.... -
JAVA获取IP地址
2021-11-11 11:38:421、nginx 代理后客户端获取不到真实的ip需要nginx添加配置 location /api { proxy_pass http://127.0.0.1:8181/; #以下为新增 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; #获取...
收藏数
55,942
精华内容
22,376