웹사이트의 내용을 긁어오는 Webspider

Posted by 겨울에
2011. 6. 27. 14:06 scrap/ Java/JSP

http://blog.naver.com/nicekkong/100038826513
[출처] 웹사이트의 내용을 긁어오는 Webspider|작성자 nicekkong  


import java.net.*;
import java.io.*;


public class WebSpider
{
 public static void main(String arg[])  {
  URL url = null;
  try  {
   url = new URL(arg[0]);
  } catch (MalformedURLException el) {
   System.out.println(el);
   System.exit(1);
  }
  
  FileOutputStream fos = null;
  try {
   InputStream in = url.openStream();      // 웹 문서를 openStream() 으로 읽어 온다. 
   fos = new FileOutputStream("test.txt");
   
   byte[] buffer = new byte[512];
   int readcount = 0;
   
   System.out.println("읽어오기 시작합니다.");
   while((readcount = in.read(buffer)) != -1)  {
    fos.write(buffer, 0, readcount);
   }
   System.out.println("finish!");
  } catch (Exception ex) {
   System.out.println(ex);
  } finally {
   try {
    if(fos != null) fos.close();
   } catch(Exception e) {}
  }
 }
}