java 获取外网ip

本程序通过Java实现网页信息抓取,能从指定网址获取源代码并解析出IP地址及地理位置等信息,最后将结果显示在GUI窗口中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package Test;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class CatchWeb {
/**
* 获取网站源码
* @param basePath
* @param childPath
* @return
* @throws SocketTimeoutException
*/
public String getWebSource(String basePath, String childPath){
StringBuffer sb = new StringBuffer("");
try {
URL url = new URL(basePath + childPath);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// con.setFollowRedirects(true);
con.setInstanceFollowRedirects(false);
con.setConnectTimeout(20000);
con.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(con
.getInputStream()));
String s = "";
while ((s = br.readLine()) != null) {
sb.append(s + "\r\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}

/********************* 匹配信息的方法 **********************/
public String getCity(String source) {
if (source != null) {
source = source.substring(source.indexOf("来自"), source
.indexOf("<br/>"));
}
return source;
}

public String getIP(String source) {
if (source != null) {
source = source.substring(source.indexOf("[") + 1, source
.indexOf("]"));
}
return source;
}


public static void main(String[] args) {
CatchWeb catchWeb = new CatchWeb();

/**************** JFrame设置 *******************/
JFrame jf = new JFrame("获取IP");
JPanel jp = new JPanel();
JLabel label = new JLabel();

label.setFont(new Font("微软雅黑", Font.LAYOUT_LEFT_TO_RIGHT, 12));
jp.add(label);
jf.add(jp);
jf.setVisible(true);
jf.setBounds(400, 200, 200, 80);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/**************** 连接设置 *******************/
String basePath = "http://www.ip138.com/";
String ipurl = "ip2city.asp";
String cityurl = "ips.asp";
String ip = null;
String city = null;

try{
label.setText("连接中...");
ip = catchWeb.getIP(catchWeb.getWebSource(basePath, ipurl));
city = catchWeb.getCity(catchWeb.getWebSource(basePath, cityurl
+ "?" + ip));
}catch(Exception e){
label.setText("连接超时,请检查网络!");
label.repaint();
}

if(ip != null && city != null){
label.setText("<html>您的IP是:" + ip + "<br/>" + city + "</html>");
label.repaint();
}
// System.out.println("您的IP是:" + ip + "\r\n" + city);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值