본문 바로가기

Java SE/URLConnection 04

URLConnection 04

/* URL, URLConnection 을 이용한 웹상의 파일(HTML, Image, swf 등)복사해 오기.
* URL클래스를 이용하여 URLConnection객체를 얻고, URLConnection.getInputStream()을 호출하면,
* InputStream 을 얻을 수 있다. 이 스트림을 이용하여 웹상에서 임의의 swf 파일을 복사해서
* 자신의 로컬 시스템에 저장해 보세요.
*/

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

class  URLConnectionTest04 {
 public static void main(String[] args) throws IOException  {

  URL url = new URL("http://images.hangame.co.kr/hangame/renewal_2007/www/flash/gage/HG_gage.swf");
  URLConnection conn = url.openConnection();

  InputStream in = conn.getInputStream();

  BufferedInputStream bin = new BufferedInputStream(in);
  byte[] buf = new byte[1024];
 
  ByteArrayOutputStream bao = new ByteArrayOutputStream();
 
  int read = 0;
  while((read=bin.read(buf, 0, buf.length))!= -1) {
   bao.write(buf, 0, read);
  }
  FileOutputStream fos = new FileOutputStream("hangame.swf");
  fos.write(bao.toByteArray());

  bin.close();
  bao.close();
  fos.close();
 }
}