java中获取公网IP
- package com.dashan.utils.iputils;
- import org.apache.commons.lang.StringUtils;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * @author ADMIN
- */
- public class IPGWUtil {
- // 方法1
- private String getNowIP1() throws IOException {
- String ip = null;
- 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");
- }
- Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
- Matcher m = p.matcher(inputLine.toString());
- if (m.find()) {
- String ipstr = m.group(1);
- ip = ipstr;
- }
- } finally {
- if (in != null) {
- in.close();
- }
- }
- if (StringUtils.isEmpty(ip)) {
- throw new RuntimeException();
- }
- return ip;
- }
- // 方法2
- private String getNowIP2() throws IOException {
- String ip = null;
- BufferedReader br = null;
- try {
- URL url = new URL("https://v6r.ipip.net/?format=callback");
- br = new BufferedReader(new InputStreamReader(url.openStream()));
- String s = "";
- StringBuffer sb = new StringBuffer("");
- String webContent = "";
- while ((s = br.readLine()) != null) {
- sb.append(s + "\r\n");
- }
- webContent = sb.toString();
- int start = webContent.indexOf("(") + 2;
- int end = webContent.indexOf(")") - 1;
- webContent = webContent.substring(start, end);
- ip = webContent;
- } finally {
- if (br != null) {
- br.close();
- }
- }
- if (StringUtils.isEmpty(ip)) {
- throw new RuntimeException();
- }
- return ip;
- }
- // 方法3
- private String getNowIP3() throws IOException {
- String ip = null;
- String objWebURL = "https://ip.900cha.com/";
- BufferedReader br = null;
- try {
- URL url = new URL(objWebURL);
- br = new BufferedReader(new InputStreamReader(url.openStream()));
- String s = "";
- String webContent = "";
- while ((s = br.readLine()) != null) {
- if (s.indexOf("我的IP:") != -1) {
- ip = s.substring(s.indexOf(":") + 1);
- break;
- }
- }
- } finally {
- if (br != null) {
- br.close();
- }
- }
- if (StringUtils.isEmpty(ip)) {
- throw new RuntimeException();
- }
- return ip;
- }
- // 方法4
- private String getNowIP4() throws IOException {
- String ip = null;
- String objWebURL = "https://bajiu.cn/ip/";
- BufferedReader br = null;
- try {
- URL url = new URL(objWebURL);
- br = new BufferedReader(new InputStreamReader(url.openStream()));
- String s = "";
- String webContent = "";
- while ((s = br.readLine()) != null) {
- if (s.indexOf("互联网IP") != -1) {
- ip = s.substring(s.indexOf("'") + 1, s.lastIndexOf("'"));
- break;
- }
- }
- } finally {
- if (br != null) {
- br.close();
- }
- }
- if (StringUtils.isEmpty(ip)) {
- throw new RuntimeException();
- }
- return ip;
- }
- // 返回公网地址
- public static String findGWIp() {
- IPGWUtil ipgwUtil = new IPGWUtil();
- String ip = null;
- // 第一种方式
- try {
- ip = ipgwUtil.getNowIP1();
- ip.trim();
- } catch (Exception e) {
- System.out.println("getPublicIP - getNowIP1 failed ~ ");
- }
- if (!StringUtils.isEmpty(ip)) {
- return ip;
- }
- // 第二种方式
- try {
- ip = ipgwUtil.getNowIP2();
- ip.trim();
- } catch (Exception e) {
- System.out.println("getPublicIP - getNowIP2 failed ~ ");
- }
- if (!StringUtils.isEmpty(ip)) {
- return ip;
- }
- // 第三种方式
- try {
- ip = ipgwUtil.getNowIP3();
- ip.trim();
- } catch (Exception e) {
- System.out.println("getPublicIP - getNowIP3 failed ~ ");
- }
- if (!StringUtils.isEmpty(ip)) {
- return ip;
- }
- // 第四种方式
- try {
- ip = ipgwUtil.getNowIP4();
- ip.trim();
- } catch (Exception e) {
- System.out.println("getPublicIP - getNowIP4 failed ~ ");
- }
- if (!StringUtils.isEmpty(ip)) {
- return ip;
- }
- return ip;
- }
- public static void main(String[] args) {
- System.out.println(IPGWUtil.findGWIp());
- }
- }
java中获取公网IP的更多相关文章
- java中获取远程ip的一个坑
发现在高请求量的时候获取hostName慢,后发现getHostName方法慢导致的:需要获取hostName为获取ip的方式了:java 中 InetSocketAddress // remoteA ...
- JAVA获取公网ip
在ipv4地址稀缺的今天,分配到公网ip几乎是不可能的,但是我拨号之后的ip竟然是公网IP. 将自己的电脑作为服务器·,做点好玩的程序,就成为了可能. 由于运营商的ip是动态分配的公网ip的所以就需要 ...
- Python 之自动获取公网IP
Python 之自动获取公网IP 2017年9月30日 文档下载:https://wenku.baidu.com/view/ff40aef7f021dd36a32d7375a417866fb84ac0 ...
- JAVA中获取当前系统时间及格式转换
JAVA中获取当前系统时间 一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...
- C#联机获取公网IP
C#获取IP的方式有很多种,这里通过http://www.ipip.net/这个稳定的在线IP库的来获取公网IP. string tempip = "0.0.0.0"; WebRe ...
- Java中获取键盘输入值的三种方法
Java中获取键盘输入值的三种方法 Java程序开发过程中,需要从键盘获取输入值是常有的事,但Java它偏偏就没有像c语言给我们提供的scanf(),C++给我们提供的cin()获取键盘输入值 ...
- JAVA中获取路径
内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径: 以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.get ...
- java中获取日期和时间的方法总结
1.获取当前时间,和某个时间进行比较.此时主要拿long型的时间值. 方法如下: 要使用 java.util.Date .获取当前时间的代码如下 Date date = new Date(); da ...
- JAVA中获取当前系统时间
一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowStrin ...
- java中获取路径中的空格处理(%20)问题
在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getReso ...
随机推荐
- Vocabulary
词汇(Vocabulary) blackmail ( n.) :the obtaining of money or advancement by threatening to make known u ...
- Go通道机制与应用详解
本文深入探讨了Go语言中通道(Channel)的各个方面,从基础概念到高级应用.文章详细解析了通道的类型.操作方法以及垃圾回收机制,更进一步通过具体代码示例展示了通道在数据流处理.任务调度和状态监控等 ...
- linux bond nmcli命令
基本命令: nmcli connection show 显示所有连接nmcli connection show --active 显示所有活动的连接状态nmcli connection show &q ...
- IL编制器 --- Fody
介绍 这个项目的名称"Fody"来源于属于织巢鸟科(Ploceidae)的小鸟(Fody),本身意义为编织. 核心Fody引擎的代码库地址 :https://github.com/ ...
- CSS 元素居中方式总结
作者:WangMin 格言:努力做好自己喜欢的每一件事 在开发过程中,很多网页需求要求我们居中一个div,比如html文档流当中的一块div,比如弹出层内容部分这种脱离了文档流等.不同的情况有不同的居 ...
- 万字解析XML配置映射为BeanDefinition的源码
本文分享自华为云社区<Spring高手之路16--解析XML配置映射为BeanDefinition的源码>,作者:砖业洋__. 1. BeanDefinition阶段的分析 Spring框 ...
- MySQL概述安装
一,数据库概述 1.为什么要使用数据库 将数据持久化. 持久化主要作用:是将内存中的数据库存储在关系型数据库中,本质也就是存储在磁盘文件中. 数据库在横向上的存储数据的条数,以及在纵向上存储数据的丰富 ...
- IDEA提示Cannot resolve method 'getContextPath()'
一.问题原因: 二.解决方案: 1.打开Project Structure 2.new一个新的Java的project library文件 3.选择tomcat路径下的lib文件夹. 三.完成 可以看 ...
- CentOS(7.6)环境下迁移Mysql(5.7)的data目录到指定位置
第一步:关闭Mysql #关闭Mysql服务systemctl stop mysqld#查看Mysql服务状态 ps -ef|grep mysql 第二步:创建新目录,并拷贝数据文件 #创建data文 ...
- [HAOI2018] 字串覆盖
[HAOI2018]字串覆盖 题目描述 小C对字符串颇有研究,他觉得传统的字符串匹配太无聊了,于是他想到了这 样一个问题. 对于两个长度为n的串A, B, 小C每次会给出给出4个参数s, t, l, ...