mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理


package com.fang.news.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;


/**   
 *    
 * 项目名称:NewsPortalProject   
 * 类名称:HTMLMaker   
 * 类描写叙述:   
 * 创建人:XianJuanJuan   
 * 创建时间:2015年7月23日 下午5:34:02    
 * @version    
 *    
 */
public class HTMLMaker {
	
	final static Object lock = new Object();   
	public static void makeHtml(String page, String filePath){   
		makeHtml(page,filePath,"utf-8");   
	 }   

	public static void main(String[] args) {
		
		String page = "http://bj.58.com/";
		String filePath = "D://test.html";
		makeHtml(page, filePath);
	}
	 public static void makeHtml(String page, String filePath,String chartset) {   
	  synchronized (lock) {
		    
		  HttpURLConnection huc = null;   
		  BufferedReader br = null;   
		  BufferedWriter bw = null;   
		  try {   
			  huc = (HttpURLConnection)new URL(page).openConnection();   
			  System.setProperty("sun.net.client.defaultConnectTimeout", "30000");   
			  System.setProperty("sun.net.client.defaultReadTimeout", "30000");    
			  huc.connect();   
			  InputStream stream = huc.getInputStream();   
			  bw = new BufferedWriter(new OutputStreamWriter (new FileOutputStream(filePath),chartset));   
			  br = new BufferedReader(new InputStreamReader(stream, chartset));   
			  String line;   
			  while((line = br.readLine())!= null){   
				  if(line.trim().length() > 0){   
					  bw.write(line);   
					  bw.newLine();   
				  }   
			  }   
		  } catch (Exception e) {   
			  e.printStackTrace();   
		  } finally {   
			  try {   
				  br.close();   
				  bw.close();   
				  huc.disconnect();   
			  } catch (Exception e) {   
				  e.printStackTrace();   
			  }   
		  }    
	  	}   
	 }
}


posted on 2017-06-01 20:55  mthoutai  阅读(242)  评论(0编辑  收藏  举报