如今买票是一大难事,在高峰时段 打开12306网站,慢的像蜗牛,想到以前用修改hosts文件来登录Google(Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应网页,如果没有找到,则系统再会将网址提交DNS域名解析服务器进行IP地址的解析。),那么我也来试试这种方法来登录下12306,用搜索找到几个解析IP的网站,试了下,果然奏效(现在).为了方便使用,我把IP写到本地文件.

下面是JAVA的相关代码,显示把IP写到D盘的ip.txt中.

package Demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class testREG_IP { // 指定文件路径和名称
private static String path = "D:/ip.txt";
private static File fileName = new File(path);
private static String readStr = "";
private static BufferedReader bufferReader; public static void main(String[] args) throws IOException {
//网址
String path = "http://www.17ce.com/site/http/201401_17ccef99e3d1ca9c19aea3c3d19136dc.html";
//正则表达式,过滤IP格式
String regex = "\\d+\\.\\d+\\.\\d+\\.\\d+";
String ipText = getWeb(path, regex);
createTxtFile();
readTxtFile();
System.out.println(ipText);
writeTxtFile(ipText);
}
/**
* 创建文件
*
* @throws IOException
*/
private static void createTxtFile() throws IOException {
if (!fileName.exists()) {
fileName.createNewFile();
System.err.println(fileName + " 已创建!");
}
} /**
* 读文件
*/
public static String readTxtFile() {
String read;
FileReader fileread;
try {
fileread = new FileReader(fileName);
bufferReader = new BufferedReader(fileread);
try {
while ((read = bufferReader.readLine()) != null) {
readStr = readStr + read + "\r\n";
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return readStr;
} /**
* 写文件
*/
public static void writeTxtFile(String newStr) throws IOException {
// 追加内容
String filein = newStr + "\r\n" + readStr + "\r\n";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(fileName, "rw");
mm.writeBytes(filein);
System.out.println("写入成功");
} catch (IOException e1) {
System.out.println("写入失败");
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
System.out.println("写入失败");
e2.printStackTrace();
}
}
}
}
//获取ip
public static String getWeb(String ip, String regex) {
URL url;
final StringBuffer stringBuffer = new StringBuffer();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = formatter.format(new Date());
stringBuffer.append("\r\n----------" + time + "-----------\r\n\r\n\r\n");
try {
url = new URL(ip);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.connect();
InputStream in = urlConnection.getInputStream();
byte[] data = new byte[1024];
while (in.read(data) > 0) { // 读取流文件
String tempString = new String(data);
Matcher m = Pattern.compile(regex).matcher(tempString);
while (m.find()) {
stringBuffer.append(m.group());
stringBuffer.append("\r\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();
} }

JAVA 获取指定网址的IP地址 实例的更多相关文章

  1. Java 获取客服端ip地址

    Java 获取客服端ip地址 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p& ...

  2. java 获取访问主机的ip地址

    Java的api的说法: getHeader public java.lang.String getHeader(java.lang.String name) Return the first val ...

  3. java获取当前网站的IP地址

    package ip; import java.net.InetAddress; import java.net.UnknownHostException; /** * * @author * */ ...

  4. java获取访问者真实的IP地址

    众所周知java方法request.getremoteaddr()可以获得访问者的IP地址 但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了.如果使用了反向代理软件 ...

  5. java获取登陆用户的IP地址

    /** * 通过HttpServletRequest返回IP地址 * @param request HttpServletRequest * @return ip String * @throws E ...

  6. java 获取本机的IP地址

    方法一:这种方式有一定的局限性,在Linux下的执行结果是:本机的IP = xxx/127.0.1.1 (其中xxx是你的计算机名) public void getLocalIPAddress() { ...

  7. java 获取本机所有IP地址

    import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import ...

  8. java获取系统当前服务器IP地址

    public String getServiceIp(){ InetAddress address; String myIp; try { address = InetAddress.getLocal ...

  9. Java如何获取本地计算机的IP地址和主机名?

    在Java编程中,如何获取本地计算机的IP地址和主机名? 以下示例显示如何使用InetAddress类的getLocalAddress()方法获取系统的本地IP地址和主机名. package com. ...

随机推荐

  1. hibernate 的sum(filed)引发的NullPointException错误解决过程

    背景: 在用hql语句进行sum查询时遭遇NPE问题: StringBuilder builder = new StringBuilder("select SUM(actualWorking ...

  2. Form的is_valid校验规则及验证顺序

    一.验证顺序   查看form下的源码了解顺序 BaseForm为基类,中间包含了is_valid校验方法 @html_safe class BaseForm: ......... self.is_b ...

  3. Golang的类型断言

    类型断言即判断一个变量是不是某个类型的实例,这个经常用在判断接口的类型,基本的格式: y, ok := x.(type) 上面的语句用于判断变量x是不是type类型,有两种结果: x是type类型的变 ...

  4. IntelliJ IDEA/WebStrom破解及JDK配置

    title: IntelliJ IDEA/WebStrom破解及JDK配置 (一)破解 破解步骤 第一步:下载破解补丁 第二步:修改配置文件 第三步:重启IntelliJ IDEA/WebStrom填 ...

  5. 原生JS操作object HTMLTableSectionElement 对象,获取行数

    html页面 <tbody id="infoTab"> <tr class="fomat"> <td class="bl ...

  6. [转帖]Gartner预测:2019年全球公有云规模达2143亿美元

    Gartner预测:2019年全球公有云规模达2143亿美元 https://news.cnblogs.com/n/623341/ 近日,全球领先的信息技术研究和顾问公司 Gartner 发布的最新数 ...

  7. [转帖]2015年时微软Win3.1崩溃迫使巴黎奥利机场短暂关闭

    https://www.ithome.com/html/it/188796.htm IT之家讯 2015年11月14日消息,上周法国巴黎奥利机场因为微软的Windows 3.1系统出现故障不得不迫使所 ...

  8. vant的坑

    1.轮播图设置, .img { width: 100%; height: 100%; object-fit: cover; touch-action: none; } 如果不设置不能达到 保持纵横比缩 ...

  9. .Net MVC4 log4net的配置

    一.首先在使用log4net记录日志的时候,我们要引用log4net.dll文件 二.在web.config中添加一下配置代码 <configSections> <!-- For m ...

  10. PS中如何把图片颜色加到字体上去

    1.在PS中的图层中,将图片置于文字层的上方,同时按ctrl+alt+g键,这样就将文字范围以外的图像给剪切掉了.见附图下方的效果. 2.最终效果如下图: 参见:https://zhidao.baid ...