⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nethelper.java

📁 利用多线程从搜索引擎下载网页并提取数据到数据库。
💻 JAVA
字号:
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.mysql.jdbc.Connection;

public class NetHelper {

	/**
	 * @param args
	 */
	static Process lastP = null;
	static Socket soc;
	static PrintWriter dos;
	private static int count;

	static boolean ipMode;

	public static void main(String[] args) throws UnknownHostException,
			IOException {
		// test();
		notifyDial();
	}

	private static void test() throws UnknownHostException, IOException {
		while (true) {
			restartNet();
			try {
				Thread.sleep(30000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void restartNet() throws UnknownHostException, IOException {
		int count = 0;
		int maxTry = 10;
		if (lastP != null)
			lastP.destroy();
		try {
			Thread.sleep(500);
		} catch (InterruptedException e2) {
			e2.printStackTrace();
		}
		switchIP();
		do {
			try {
				lastP = startSSH();
			} catch (IOException e1) {
				e1.printStackTrace();
				lastP = null;
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
			}
		} while (lastP == null && count++ < maxTry);
		if (lastP == null)
			throw new IOException("Failed to establish SSH!");
		try {
			// it takes a few seconds to connect to host.
			Thread.sleep(2000);
		} catch (InterruptedException e) {
		}
	}

	/**
	 * alternatives: socket, jni. they can be faster
	 * 
	 * @throws IOException
	 * @throws UnknownHostException
	 */

	public synchronized static void notifyDial() throws UnknownHostException,
			IOException {

		notifyThruSocket();

	}

	private static void notifyThruSocket() throws UnknownHostException,
			IOException {
		// if (soc == null)
		{
			soc = new Socket("localhost", 10000);
			soc.setKeepAlive(true);
			dos = new PrintWriter(soc.getOutputStream());
		}
		System.out.println("Writing 'dial'" + count++);
		dos.println("dial");
		dos.flush();
		dos.close();
		soc.close();
	}

	private static void notifyThruFile() {
		FileWriter fr = null;
		BufferedWriter bw = null;
		try {
			fr = new FileWriter("c:\\command.txt");
			bw = new BufferedWriter(fr);
			bw.write("Dial");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bw.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void switchIP() {
		try {
			notifyDial();
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		if (ipMode) {
			addr = "http://www.hjjok.cn/admin/showip.asp";
			while (!ipChanged()) {
				try {
					notifyDial();
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			addr = "http://www.hjj.cn";
			String page = null;
			while (page == null) {
				try {
					page = Util.getAPage(addr, 2000);
					notifyDial();
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static String lastIP = "";

	private static boolean ipChanged() {
		String newIP;
		int count;
		count = 0;
		do {
			newIP = parseIP();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} while (newIP == null && count++ < 3);
		boolean result;
		result = (!lastIP.equals(newIP) && newIP != null);
		if (result)
			lastIP = newIP;
		return result;
	}

	static String addr = "http://www.hjjok.cn/admin/showip.asp";

	// static String addr = "http://www.hjj.cn";
	public static String parseIP() {
		String page;
		try {
			page = Util.getAPage(addr, 2000);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		String regx = "(\\d{1,3}.){3}\\d{1,3}";
		Pattern p = Pattern.compile(regx);
		Matcher m = p.matcher(page);
		if (m.find())
			return m.group();
		else
			return null;
	}

	public static Process startSSH() throws IOException {
		Process p = null;
		String cmd;
		cmd = "putty -load cpaths -l cpaths -pw buckin8t0r";
		p = Runtime.getRuntime().exec(cmd);
		// return p;
		/*
		 * byte[] b=new byte[1000]; int i; BufferedInputStream bis=new
		 * BufferedInputStream(p.getInputStream()); try { while ((i=bis.read(b,
		 * 0, 1000))>0) { System.out.println(new String(b,0,i)); } } catch
		 * (IOException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } /*
		 * 
		 */
		// try {
		// Thread.sleep(10000);
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
		// BufferedOutputStream bos = new
		// BufferedOutputStream(p.getOutputStream());
		// // p.destroy();
		// byte []b;
		// //b=new byte[]{1,2,3};
		// b="exit".getBytes();
		// try {
		// bos.write(b);
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		return p;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -