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

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


01.import java.io.BufferedInputStream;
02.import java.io.IOException;
03.import java.net.URL;
04.import java.net.URLConnection;
05. 
06.public class TestWebSourceGathering {
07.    public static void main(String[] args) {
08.        BufferedInputStream in = null;
09.        String strUrl = "http://www.google.com/";
10.        StringBuffer sb = new StringBuffer();
11.         
12.        try {
13.            URL url = new URL(strUrl);
14.            URLConnection urlConnection = url.openConnection();
15.            in = new BufferedInputStream(urlConnection.getInputStream());
16.             
17.            byte[] bufRead = new byte[4096];
18.            int lenRead = 0;
19.            while ((lenRead = in.read(bufRead)) > 0)
20.                sb.append(new String(bufRead, 0, lenRead));
21. 
22.        } catch (IOException ioe) {
23.            ioe.printStackTrace();
24.        }
25.         
26.        System.out.println(sb.toString());     
27.    }
28.}

Posted by Huikyun