package com.liness.util;
import org.springframework.util.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;
/**
* 获取IP工具类
*/
public class IpToolUtils {
// 方法1
private static 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 static 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 static 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 static 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 getPublicIP() {
String ip = null;
// 第一种方式
try {
ip = getNowIP1();
ip.trim();
} catch (Exception e) {
System.out.println("getPublicIP - getNowIP1 failed ~ ");
}
if (!StringUtils.isEmpty(ip))
return ip;
// 第二种方式
try {
ip = getNowIP2();
ip.trim();
} catch (Exception e) {
System.out.println("getPublicIP - getNowIP2 failed ~ ");
}
if (!StringUtils.isEmpty(ip))
return ip;
// 第三种方式
try {
ip = getNowIP3();
ip.trim();
} catch (Exception e) {
System.out.println("getPublicIP - getNowIP3 failed ~ ");
}
if (!StringUtils.isEmpty(ip))
return ip;
// 第四种方式
try {
ip = getNowIP4();
ip.trim();
} catch (Exception e) {
System.out.println("getPublicIP - getNowIP4 failed ~ ");
}
if (!StringUtils.isEmpty(ip))
return ip;
return ip;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该源码主要是通过java StringBoot 项目编写的一个动态修改DDNS的工具类,每隔15秒回去网上获取一次外网IP。 使用时在ddns\src\main目录下找到application.yml配置文件然后更改成自己的信息 ddns: aliyun: domain: xxxxxxx.com #主域名 regionId: cn-shenzhen #地域信息 keyword: api #解析-主机记录 accessKeyId: L111r11111111111HyR11WZ accessKeySecret: 1111111111111dy1v1g11111M11Vah
资源详情
资源评论
资源推荐
收起资源包目录
ddns-阿里云动态修改ddns.zip (16个子文件)
ddns
pom.xml 3KB
LICENSE 1KB
src
test
main
resources
application.yml 386B
java
com
liness
ddns
service
DdnsService.java 122B
impl
DdnsServiceImpl.java 4KB
task
DdnsTask.java 980B
AppStart.java 386B
util
DdnsUtils.java 2KB
IpToolUtils.java 5KB
.idea
misc.xml 569B
jarRepositories.xml 2KB
encodings.xml 314B
compiler.xml 867B
workspace.xml 3KB
.gitignore 43B
.gitignore 145B
共 16 条
- 1
YU3423
- 粉丝: 5
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0