1. package com.dashan.utils.iputils;
  2.  
  3. import org.apache.commons.lang.StringUtils;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13. /**
  14. * @author ADMIN
  15. */
  16. public class IPGWUtil {
  17. // 方法1
  18. private String getNowIP1() throws IOException {
  19. String ip = null;
  20. String chinaz = "http://ip.chinaz.com";
  21. StringBuilder inputLine = new StringBuilder();
  22. String read = "";
  23. URL url = null;
  24. HttpURLConnection urlConnection = null;
  25. BufferedReader in = null;
  26. try {
  27. url = new URL(chinaz);
  28. urlConnection = (HttpURLConnection) url.openConnection();
  29. in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
  30. while ((read = in.readLine()) != null) {
  31. inputLine.append(read + "\r\n");
  32. }
  33. Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
  34. Matcher m = p.matcher(inputLine.toString());
  35. if (m.find()) {
  36. String ipstr = m.group(1);
  37. ip = ipstr;
  38. }
  39. } finally {
  40. if (in != null) {
  41. in.close();
  42. }
  43. }
  44. if (StringUtils.isEmpty(ip)) {
  45. throw new RuntimeException();
  46. }
  47. return ip;
  48. }
  49.  
  50. // 方法2
  51. private String getNowIP2() throws IOException {
  52. String ip = null;
  53. BufferedReader br = null;
  54. try {
  55. URL url = new URL("https://v6r.ipip.net/?format=callback");
  56. br = new BufferedReader(new InputStreamReader(url.openStream()));
  57. String s = "";
  58. StringBuffer sb = new StringBuffer("");
  59. String webContent = "";
  60. while ((s = br.readLine()) != null) {
  61. sb.append(s + "\r\n");
  62. }
  63. webContent = sb.toString();
  64. int start = webContent.indexOf("(") + 2;
  65. int end = webContent.indexOf(")") - 1;
  66. webContent = webContent.substring(start, end);
  67. ip = webContent;
  68. } finally {
  69. if (br != null) {
  70. br.close();
  71. }
  72. }
  73. if (StringUtils.isEmpty(ip)) {
  74. throw new RuntimeException();
  75. }
  76. return ip;
  77. }
  78.  
  79. // 方法3
  80. private String getNowIP3() throws IOException {
  81. String ip = null;
  82. String objWebURL = "https://ip.900cha.com/";
  83. BufferedReader br = null;
  84. try {
  85. URL url = new URL(objWebURL);
  86. br = new BufferedReader(new InputStreamReader(url.openStream()));
  87. String s = "";
  88. String webContent = "";
  89. while ((s = br.readLine()) != null) {
  90. if (s.indexOf("我的IP:") != -1) {
  91. ip = s.substring(s.indexOf(":") + 1);
  92. break;
  93. }
  94. }
  95. } finally {
  96. if (br != null) {
  97. br.close();
  98. }
  99. }
  100. if (StringUtils.isEmpty(ip)) {
  101. throw new RuntimeException();
  102. }
  103. return ip;
  104. }
  105.  
  106. // 方法4
  107. private String getNowIP4() throws IOException {
  108. String ip = null;
  109. String objWebURL = "https://bajiu.cn/ip/";
  110. BufferedReader br = null;
  111. try {
  112. URL url = new URL(objWebURL);
  113. br = new BufferedReader(new InputStreamReader(url.openStream()));
  114. String s = "";
  115. String webContent = "";
  116. while ((s = br.readLine()) != null) {
  117. if (s.indexOf("互联网IP") != -1) {
  118. ip = s.substring(s.indexOf("'") + 1, s.lastIndexOf("'"));
  119. break;
  120. }
  121. }
  122. } finally {
  123. if (br != null) {
  124. br.close();
  125. }
  126. }
  127. if (StringUtils.isEmpty(ip)) {
  128. throw new RuntimeException();
  129. }
  130. return ip;
  131. }
  132.  
  133. // 返回公网地址
  134. public static String findGWIp() {
  135. IPGWUtil ipgwUtil = new IPGWUtil();
  136. String ip = null;
  137. // 第一种方式
  138. try {
  139. ip = ipgwUtil.getNowIP1();
  140. ip.trim();
  141. } catch (Exception e) {
  142. System.out.println("getPublicIP - getNowIP1 failed ~ ");
  143. }
  144. if (!StringUtils.isEmpty(ip)) {
  145. return ip;
  146. }
  147. // 第二种方式
  148. try {
  149. ip = ipgwUtil.getNowIP2();
  150. ip.trim();
  151. } catch (Exception e) {
  152. System.out.println("getPublicIP - getNowIP2 failed ~ ");
  153. }
  154. if (!StringUtils.isEmpty(ip)) {
  155. return ip;
  156. }
  157. // 第三种方式
  158. try {
  159. ip = ipgwUtil.getNowIP3();
  160. ip.trim();
  161. } catch (Exception e) {
  162. System.out.println("getPublicIP - getNowIP3 failed ~ ");
  163. }
  164. if (!StringUtils.isEmpty(ip)) {
  165. return ip;
  166. }
  167. // 第四种方式
  168. try {
  169. ip = ipgwUtil.getNowIP4();
  170. ip.trim();
  171. } catch (Exception e) {
  172. System.out.println("getPublicIP - getNowIP4 failed ~ ");
  173. }
  174. if (!StringUtils.isEmpty(ip)) {
  175. return ip;
  176. }
  177. return ip;
  178. }
  179.  
  180. public static void main(String[] args) {
  181. System.out.println(IPGWUtil.findGWIp());
  182. }
  183. }

java中获取公网IP的更多相关文章

  1. java中获取远程ip的一个坑

    发现在高请求量的时候获取hostName慢,后发现getHostName方法慢导致的:需要获取hostName为获取ip的方式了:java 中 InetSocketAddress // remoteA ...

  2. JAVA获取公网ip

    在ipv4地址稀缺的今天,分配到公网ip几乎是不可能的,但是我拨号之后的ip竟然是公网IP. 将自己的电脑作为服务器·,做点好玩的程序,就成为了可能. 由于运营商的ip是动态分配的公网ip的所以就需要 ...

  3. Python 之自动获取公网IP

    Python 之自动获取公网IP 2017年9月30日 文档下载:https://wenku.baidu.com/view/ff40aef7f021dd36a32d7375a417866fb84ac0 ...

  4. JAVA中获取当前系统时间及格式转换

    JAVA中获取当前系统时间   一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...

  5. C#联机获取公网IP

    C#获取IP的方式有很多种,这里通过http://www.ipip.net/这个稳定的在线IP库的来获取公网IP. string tempip = "0.0.0.0"; WebRe ...

  6. Java中获取键盘输入值的三种方法

    Java中获取键盘输入值的三种方法     Java程序开发过程中,需要从键盘获取输入值是常有的事,但Java它偏偏就没有像c语言给我们提供的scanf(),C++给我们提供的cin()获取键盘输入值 ...

  7. JAVA中获取路径

    内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径:   以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.get ...

  8. java中获取日期和时间的方法总结

    1.获取当前时间,和某个时间进行比较.此时主要拿long型的时间值. 方法如下:  要使用 java.util.Date .获取当前时间的代码如下 Date date = new Date(); da ...

  9. JAVA中获取当前系统时间

    一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowStrin ...

  10. java中获取路径中的空格处理(%20)问题

    在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getReso ...

随机推荐

  1. Vocabulary

    词汇(Vocabulary) blackmail ( n.) :the obtaining of money or advancement by threatening to make known u ...

  2. Go通道机制与应用详解

    本文深入探讨了Go语言中通道(Channel)的各个方面,从基础概念到高级应用.文章详细解析了通道的类型.操作方法以及垃圾回收机制,更进一步通过具体代码示例展示了通道在数据流处理.任务调度和状态监控等 ...

  3. linux bond nmcli命令

    基本命令: nmcli connection show 显示所有连接nmcli connection show --active 显示所有活动的连接状态nmcli connection show &q ...

  4. IL编制器 --- Fody

    介绍 这个项目的名称"Fody"来源于属于织巢鸟科(Ploceidae)的小鸟(Fody),本身意义为编织. 核心Fody引擎的代码库地址 :https://github.com/ ...

  5. CSS 元素居中方式总结

    作者:WangMin 格言:努力做好自己喜欢的每一件事 在开发过程中,很多网页需求要求我们居中一个div,比如html文档流当中的一块div,比如弹出层内容部分这种脱离了文档流等.不同的情况有不同的居 ...

  6. 万字解析XML配置映射为BeanDefinition的源码

    本文分享自华为云社区<Spring高手之路16--解析XML配置映射为BeanDefinition的源码>,作者:砖业洋__. 1. BeanDefinition阶段的分析 Spring框 ...

  7. MySQL概述安装

    一,数据库概述 1.为什么要使用数据库 将数据持久化. 持久化主要作用:是将内存中的数据库存储在关系型数据库中,本质也就是存储在磁盘文件中. 数据库在横向上的存储数据的条数,以及在纵向上存储数据的丰富 ...

  8. IDEA提示Cannot resolve method 'getContextPath()'

    一.问题原因: 二.解决方案: 1.打开Project Structure 2.new一个新的Java的project library文件 3.选择tomcat路径下的lib文件夹. 三.完成 可以看 ...

  9. CentOS(7.6)环境下迁移Mysql(5.7)的data目录到指定位置

    第一步:关闭Mysql #关闭Mysql服务systemctl stop mysqld#查看Mysql服务状态 ps -ef|grep mysql 第二步:创建新目录,并拷贝数据文件 #创建data文 ...

  10. [HAOI2018] 字串覆盖

    [HAOI2018]字串覆盖 题目描述 小C对字符串颇有研究,他觉得传统的字符串匹配太无聊了,于是他想到了这 样一个问题. 对于两个长度为n的串A, B, 小C每次会给出给出4个参数s, t, l, ...