1 package com.zbmon;
   2 
   3 import java.io.File;
   4 import java.io.FileOutputStream;
   5 import java.io.IOException;
   6 import java.io.InputStream;
   7 import java.net.MalformedURLException;
   8 import java.net.URL;
   9 
  10 /**
  11  * util to download file
  12  *
  13  * @author KIM, KYUNG-IL
  14  * @version 1.0
  15  */
  16 public class DownloadUtil {
  17   private DownloadUtil() {
  18   }
  19 
  20   public static void download(String url, String file) {
  21     download(url, new File(file));
  22   }
  23 
  24   public static void download(String url, File file) {
  25     try {
  26       download(new URL(url), file);
  27     }
  28     catch (MalformedURLException ex) {
  29       ex.printStackTrace();
  30     }
  31   }
  32 
  33   public static void download(URL url, String file) {
  34     download(url, new File(file));
  35   }
  36 
  37   public static void download(URL url, File file) {
  38     try {
  39       InputStream in = url.openStream();
  40       FileOutputStream out = new FileOutputStream(file);
  41 
  42       int ch = in.read();
  43       while (ch > -1) {
  44         out.write(ch);
  45         ch = in.read();
  46       }
  47     }
  48     catch (IOException ex) {
  49       ex.printStackTrace();
  50     }
  51   }
  52 
  53   public static void main(String[] args) {
  54     String url = "http://zbmon.com/wikix/";
  55     String file = "D:\\wikix.html";
  56 
  57     download(url, file);
  58   }
  59 }


CategoryJava

ZbmonWiki: DownloadUtil.java (2005-11-07 14:23:31에 zbmon가(이) 마지막으로 수정)