Windows Hosts For Google, Fire In The Hole!

!!!惊喜在下面!!!

1、Update Hosts

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * 更新Hosts文件
 *
 * @author MrChu
 * @version 1.0
 * @date 2015年1月16日
 * @see www.chuweibiao.com
 */
public class UpdateHosts {

	/** HTML页面路径 */
	private static final String PAGE_URL = "http://www.360kb.com/kb/2_122.html";

	/** Hosts文件路径 */
	private static final String HOSTS_PATH = "C:\\Windows\\System32\\drivers\\etc\\hosts";

	/** Hosts备份文件路径 */
	private static final String HOSTS_BAK_PATH = "C:\\Windows\\System32\\drivers\\etc\\hosts.bak";

	/**
	 * 根据页面URL获取HTML内容
	 * @param pageUrl
	 * 		页面URL
	 * @return
	 * 		HTML内容
	 */
	public static String getHtmlContent(String pageUrl) {
		StringBuffer sb = new StringBuffer();
		BufferedReader in = null;
		try {
			URL url = new URL(pageUrl);
			in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
			String temp;
			while ((temp = in.readLine()) != null) {
				sb.append(temp);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		int begin = sb.indexOf("#google hosts 2015 by 360kb.com");
		int end = sb.indexOf("#google hosts 2015 end") + 22;
		return sb.substring(begin, end).toString();
    }

	/**
	 * 转换HTML内容
	 * @param pageUrl
	 * 		页面URL
	 * @return
	 * 		转换后的HTML内容
	 */
	public static String convertHtmlContent(String pageUrl) {
		String content = getHtmlContent(pageUrl);
		content = content.replaceAll("(<br />)+?", "\r\n");
		content = content.replaceAll("    ", " ");
		content = content.replaceAll(" ", "");
		content = content.replaceAll("  ", " ");
		content = content.replaceAll("</span></p><p>	<span><span style=\"line-height:22px;\"><span>", "\r\n");
		content = content.replaceAll("<span> </span>", " ");
		content = content.replaceAll("</span> ", "");
		content = content.replaceAll("</span>", "");
		return content;
	}

	/**
	 * 读取Hosts文件
	 * @param filePath
	 * 		Hosts文件路径
	 * @return
	 * 		Hosts文件内容
	 */
	public static String readHosts(String filePath) {
		FileReader fileReader = null;
		BufferedReader bufferedReader = null;
		StringBuilder sb = new StringBuilder();
		try {
			fileReader = new FileReader(filePath);
			bufferedReader = new BufferedReader(fileReader);
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				sb.append(line).append("\r\n");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fileReader.close();
				bufferedReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	/**
	 * 更新Hosts文件
	 * @param filePath
	 * 		Hosts文件路径
	 * @param content
	 * 		Hosts文件内容
	 */
	public static void updateHosts(String filePath, String content) {
		FileWriter fileWriter = null;
		BufferedWriter bufferedWriter = null;
		try {
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}
			fileWriter = new FileWriter(filePath);
			bufferedWriter = new BufferedWriter(fileWriter);
			bufferedWriter.write(content);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fileWriter.flush();
				bufferedWriter.flush();
				fileWriter.close();
				bufferedWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		// Hosts文件备份
		String hostsContent = "";
		File file = new File(HOSTS_BAK_PATH);
		if (file.exists()) {
			hostsContent = readHosts(HOSTS_BAK_PATH);
		} else {
			hostsContent = readHosts(HOSTS_PATH);
			updateHosts(HOSTS_BAK_PATH , hostsContent);
		}

		// Hosts文件更新
		String newHostsContent = hostsContent + "\r\n" + convertHtmlContent(PAGE_URL);
		updateHosts(HOSTS_PATH, newHostsContent);
		System.out.println("\r\n▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽");
		System.out.println("\r\n  ▇▆▄▃▂▁Hosts文件更新成功!▁▂▃▄▆▇\r\n");
		System.out.println("△△△△△△△△△△△△△△△△△△△△△△△△");

		// 浏览器访问测试可行性
		try {
            URI uri = new URI("http://www.google.com/");
            Desktop.getDesktop().browse(uri);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
	}

}

2、Restore Hosts

import java.io.File;

/**
 * 还原Hosts文件
 *
 * @author MrChu
 * @version 1.0
 * @date 2015年1月16日
 * @see www.chuweibiao.com
 */
public class RestoreHosts {

	/** Hosts文件路径 */
	private static final String HOSTS_PATH = "C:\\Windows\\System32\\drivers\\etc\\hosts";

	/** Hosts备份文件路径 */
	private static final String HOSTS_BAK_PATH = "C:\\Windows\\System32\\drivers\\etc\\hosts.bak";

	/**
	 * 还原Hosts文件
	 * @return
	 * 		true:还原成功,false:还原失败
	 */
	public static boolean restoreHosts() {
		File file = new File(HOSTS_BAK_PATH);
		if (!file.exists()) {
			return false;
		} else {
			UpdateHosts.updateHosts(HOSTS_PATH, UpdateHosts.readHosts(HOSTS_BAK_PATH));
			return file.delete();
		}
	}

	public static void main(String[] args) {
		if (restoreHosts()) {
			System.out.println("\r\n▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽");
			System.out.println("\r\n  ▇▆▄▃▂▁Hosts文件还原成功!▁▂▃▄▆▇\r\n");
			System.out.println("△△△△△△△△△△△△△△△△△△△△△△△△");
		} else {
			System.out.println("\r\n▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽");
			System.out.println("\r\n  ▇▆▄▃▂▁还原失败,Hosts备份文件不存在!▁▂▃▄▆▇\r\n");
			System.out.println("△△△△△△△△△△△△△△△△△△△△△△△△△△△△△△");
		}
	}

}

Crossing The Great Wall(史上最简单无公害翻墙之术......):

http://download.csdn.net/detail/for_china2012/8368737

时间: 2024-12-18 02:03:17

Windows Hosts For Google, Fire In The Hole!的相关文章

变化hosts 解决Google打开速度慢的问题 Google 全球IP地址 代理站点

Google 代替网 http://sinaapp.co http://alicdn.co http://baidustatic.co http://www.aol.com/ https://startpage.com/ 一个收费的 FQ站点 9元/月 https://ybb117.com/ 怎样不让google.com跳转到google.com.hk? 来自知乎 自从google的server搬离中国大陆后,大陆地区用户用google服务时会自己主动跳转到香港的http://google.co

修改电脑HOSTS登陆Google网【仅供学习使用】--给个其他网站博客链接

HOSTS 如果最新的不可用,建议网友试试其它的,或许会有奇迹发生. 因为博主经常会用到Google相关服务,所以会较劲脑汁,不择手段在互联网上寻找可用且快速的hosts:1. 找到hosts这个文件,在Windows 系统下是位于C盘/windows/system32/drivers/etc目录里.2. 以记事本的方式打开hosts,添加下载文件中的地址并保存就可以了. 博主大神链接 蜂巢's Blog /*PS:希望博主大神不会介意小的私自将你的博客地址传出去了

Windows hosts文件内容示例

# Copyright (c) 1993-1999 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address shoul

Windows hosts (使用方法 &amp;&amp; 不定期更新)

Windows 系统hosts位于 C:\Windows\System32\drivers\etc\hosts 使用方法:删除原来的hosts文件(不放心可以剪切到其他路径备份),然后将本文链接里的hosts下载下来粘贴进去. 生效:一般是实时生效的,有的电脑是重启浏览器,有的则需要重启电脑. 说明:此hosts我自己也在用,没有更新说明最后一次更新的hosts文件还可以用.(如果确实不能使用请联系我,我会更新的=.=) /*********** 2016-07-12 **********/ h

Windows环境下google protobuf入门

我使用的是最新版本的protobuf(protobuf-2.6.1),编程工具使用VS2010.简单介绍下google protobuf: google protobuf 主要用于通讯,是google出的一个结构化信息传递工具,有着效率高.占存储少的优点,常被用于网络通讯. Google protobuf主要是针对Linux下的开发,但是为了照顾windows的开发人员,google也给出了相应的方案. windows下,需要下载两个包protobuf-2.6.1.tar.bz2和protobu

windows hosts 作用以及常见问题解决

借用百度百科的解释 Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应网页,如果没有找到,则系统会再将网址提交DNS域名解析服务器进行IP地址的解析. 更详细的说明:http://baike.baidu.com/link?url=uwQLcpconowLlQZII12bLU58-5uT_4qX

[GUIDE] How to install Scipy in Maya Windows 64 bit - Google 网上论坛 - Google Chrome

I've seen a lot of queries about getting scipy working in Maya (Windows 64 bit) with a few not 100% reproducible answers. So after a long personal struggle with the problem, here's my solution which will hopefully end the madness for all Windows Maya

Google可用的hosts文件

修改C:\WINDOWS\system32\drivers\etc\hosts文件 #Google Services START216.58.209.195 0.docs.google.com216.58.209.195 0.drive.google.com216.58.209.195 1.docs.google.com216.58.209.195 1.drive.google.com216.58.209.195 10.docs.google.com216.58.209.195 10.drive

Hosts文件的位置

Operating System Version(s) Location Unix, Unix-like, POSIX   /etc/hosts Microsoft Windows 3.1 %WinDir%\HOSTS Microsoft Windows 95, 98, ME %WinDir%\hosts Microsoft Windows NT, 2000, XP, 2003, Vista, 2008, 7, 2012, 8, 10 %SystemRoot%\System32\drivers\