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(); } } } } }