내장바구니 | 주문배송조회 | 내적립금
6만원 이상 무료배송
주문하시는
총상품금액의 합계가
6만원 이상일 경우
택배비가 무료입니다.
[JAVA] 웹소스(HTTP sources)를 로컬 파일로 다운로드하는 소스
작성자: 어라    작성일: 2009-07-07 09:45   조회: 257748   댓글: 1
import java.io.*;
import java.net.*;

/*
 * Command line program to download data from URLs and save
 * it to local files. Run like this:
 * java FileDownload http://schmidt.devlib.org/java/file-download.html
 * @author Marco Schmidt
 */
public class FileDownload {
    public static void download(String address, String localFileName) {
        OutputStream out = null;
        URLConnection conn = null;
        InputStream  in = null;
        try {
            URL url = new URL(address);
            out = new BufferedOutputStream(
                new FileOutputStream(localFileName));
            conn = url.openConnection();
            in = conn.getInputStream();
            byte[] buffer = new byte[1024];
            int numRead;
            long numWritten = 0;
            while ((numRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, numRead);
                numWritten += numRead;
            }
            System.out.println(localFileName + "\t" + numWritten);
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException ioe) {
            }
        }
    }

    public static void download(String address) {
        int lastSlashIndex = address.lastIndexOf('/');
        if (lastSlashIndex >= 0 &&
            lastSlashIndex < address.length() - 1) {
            download(address, address.substring(lastSlashIndex + 1));
        } else {
            System.err.println("Could not figure out local file name for " +
                address);
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            download(args[i]);
        }
    }
}
 
 
 * 관련 댓글 한말씀 부탁합니다.
좋은 정보 감사합니다. 잘 작동하네요 ^^
박효민
2015-10-02 20:48
  작성자:    비밀번호:   (비밀번호는 숫자 4자리이며 본인댓글 삭제시 필요합니다.)
이용약관 | 개인정보취급방침