Java/Tip & Tech2009. 7. 3. 15:04

어떠어떠한 프로젝트를 진행하다가 웹사이트의 html 소스를 다운받아야 할 일이 생겼다.
사실 이 소스를 잘 응용하면 이메일 수집기나 기타 웹에서 데이터를 수집하는
데이터 수집기를 만들 수 있지 않을까 싶다.


import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class TestWebSourceGathering {
	public static void main(String[] args) {
		BufferedInputStream in = null;
		String strUrl = "http://www.google.com/";
		StringBuffer sb = new StringBuffer();
		
		try {
			URL url = new URL(strUrl);
			URLConnection urlConnection = url.openConnection();
			in = new BufferedInputStream(urlConnection.getInputStream());
			
			byte[] bufRead = new byte[4096];
			int lenRead = 0;
			while ((lenRead = in.read(bufRead)) > 0)
				sb.append(new String(bufRead, 0, lenRead));

		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
		System.out.println(sb.toString());		
	}
}

Posted by Huikyun