写得一坨屎一样的练习v0.2

修改了一下昨天的练习,但是我不知道这修改有何意义,哪位高人来打救一下我!为啥我认识的都是损友。

package com.hxgsn.entities;

import java.util.Date;

public class ipPort {

	private String ip;// ip地址
	private int port;// 端口
	private Date errorTime;// 出错时间

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public Date getErrorTime() {
		return errorTime;
	}

	public void setErrorTime(Date errorTime) {
		this.errorTime = errorTime;
	}

}
package com.hxgsn.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.hxgsn.entities.ipPort;

public class httpProxy {

	private Properties properties = new Properties();

	private List list = new ArrayList();

	private boolean flag = true;// 防止重复请求

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public boolean isFlag() {
		System.out.println("现有的IP数量为:" + list.size());
		if (list.size() == 0 && flag) {
			System.out.println("开始更新IP列表");
			flag = false;// 禁止重复请求
			propToList();// 当集合里没数据,重新请求数据
		}
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public void getIpResult() {
		Document doc;
		try {
			doc = Jsoup.connect("http://cn-proxy.com/").get(); // 获取请求连接
			Pattern pattern = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+)(\\d+)");// 使用正则表达式取出需要的数据
			Matcher matcher = pattern.matcher(doc.toString().replaceAll("\\s*", ""));// 去除doc里的空格和换行
			Properties properties = new Properties();
			try {
				OutputStream outputStream = new FileOutputStream(getRootPath() + "/bin/ip.properties");// 创建ip.properties文件
				System.out.println("开始写入数据");
				while (matcher.find()) {
					properties.setProperty(matcher.group(1), matcher.group(2));// 添加数据到ip.properties
					System.out.println(matcher.group(1) + ":" + matcher.group(2));
				}
				properties.store(outputStream, "http://cn-proxy.com/");// 添加注释
				outputStream.close();// 关闭文件流
				System.out.println("写入数据结束");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getRootPath() {
		String result = httpProxy.class.getResource("httpProxy.class").toString().replaceAll("%20", " ");// 获取当前class的路径
		int index = result.indexOf("WEB-INF");
		if (index == -1) {
			index = result.indexOf("bin");
		}
		result = result.substring(0, index);
		if (result.startsWith("jar")) {
			result = result.substring(10);
		} else if (result.startsWith("file")) {
			result = result.substring(6);
		}
		if (!result.substring(1, 2).equals(":")) {
			result = "/" + result;
		}
		if (result.endsWith("/"))
			result = result.substring(0, result.length() - 1);
		return result;
	}

	public void propToList() {
		try {
			do {
				File file = new File(getRootPath() + "/bin/ip.properties");
				boolean exists = file.exists();// 判断ip.properties存不存在
				if (!exists) {
					getIpResult();// ip.properties不存在,调用getIpResult方法创建一个,并写入数据
				}
				InputStream inputStream = new FileInputStream(getRootPath() + "/bin/ip.properties");
				properties.load(inputStream);
				inputStream.close();
				if (properties.size() == 0) {
					getIpResult();// 判断ip.properties是否被清空,为0即已被清空,重新写入数据
				}
			} while (properties.size() == 0);// 判断ip.properties文件状态

			System.out.println("开始读取文件内容");
			Iterator> it = properties.entrySet().iterator();
			while (it.hasNext()) {
				Entry entry = it.next();
				ipPort txt = new ipPort();
				txt.setIp(entry.getKey().toString());
				txt.setPort(Integer.parseInt(entry.getValue().toString()));// Object转换为int
				list.add(txt);
			}
			Collections.shuffle(list);// 随机地打乱参数list里的元素顺序
			flag = true;// 更改请求状态
			System.out.println("读取文件内容结束");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void delIp(int num) {
		if (flag) {
			if (list.size() == 0) {
				getIpResult();// 集合被删除全部对象后,重新读取
			} else if (list.get(num).getErrorTime() == null) {
				list.get(num).setErrorTime(new Date());// 判断出错时间是否为空,为空的情况下记录当前时间
			} else {
				long dmm = new Date().getTime() - list.get(num).getErrorTime().getTime();// 计算上次出错时间与当前出错时间的时间差
				int d = (int) dmm / 1000 / 60;// 转换为分钟
				System.out.println("上一次出现错误的时间是:" + list.get(num).getErrorTime());
				System.out.println("本次出现错误的时间是:" + new Date());
				System.out.println("两次错误的时间差是:" + d + "分钟");
				if (d < 1) {
					list.remove(list.get(num));// 当时间差少于1分钟,删掉当前对象
				} else {
					list.get(num).setErrorTime(new Date());// 当时间差大于1分钟,更新出错时间为当前出错时间
				}
			}
		}
	}

	public httpProxy() {
		if (isFlag()) {// 触发初始化
			System.out.println("IP列表更新成功");
			System.out.println("现有的IP数量为:" + list.size());
		}
	}

}
package com.hxgsn.util;

import java.util.Date;

import com.hxgsn.entities.ipPort;

public class domeThread implements Runnable {

	@Override
	public void run() {

		httpProxy hp = new httpProxy();
		ipPort ip = new ipPort();

		int listSize = hp.getList().size();
		for (int i = 0; i < listSize; i++) {
			ip = hp.getList().get(i);
			System.out.println("ip" + (i + 1) + ":" + ip.getIp() + ":" + ip.getPort());
		}

		hp.getList().get(1).setErrorTime(new Date());// 插入当前时间以便测试
		int j = 0;

		while (hp.isFlag()) {
			System.out.println("定时器启动");
			j = j + 1;// 用于测试delIp方法是否正常
			try {
				System.out.println("开始执行第" + j + "次");
				hp.delIp(1);
				Thread.sleep(60 * 1000 * j);// 休眠,下一次启动时间
			} catch (InterruptedException e) {
				try {
					System.out.println("出现错误,重新执行第" + j + "次");
					hp.delIp(1);
					Thread.sleep(60 * 1000 * j);
				} catch (InterruptedException e1) {
					System.out.println("出现错误,执行第" + j + "次失败");
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
			System.out.println("ip" + ":" + hp.getList().get(1).getIp() + ":" + hp.getList().get(1).getPort() + "\t" + hp.getList().get(1).getErrorTime());
			if (j == 3) {
				System.out.println("定时器结束");
				break;// 跳出循环,结束线程
			}
		}
	}

}
package com.hxgsn.dome;

import com.hxgsn.util.domeThread;

public class dome {

	public static void main(String[] args) {

		new Thread(new domeThread()).start();// 启动线程,定时器

	}

}

分享到: