본문 바로가기

이것저것 배운것/JAVA 공부중

Static URL Path Download

Development Environment


JDK 1.8.0_45

Elipse - Mars


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

public class downloader {
    public static void main(String args[]) {
        try {
            URL u = new URL("Download File URL");
            File filePath = new File("C:\\Download\\");
			filePath.mkdirs();
			FileOutputStream fos = new FileOutputStream("C:\\Download\\FileName.extension");
			InputStream is = u.openStream();
			byte[] buf = new byte[2048];
			double len = 0;
			double tmp = 0;
			double percent = 0;
			while ((len = is.read(buf)) > 0) {
				tmp += len / 1024 / 1024;
				percent = tmp / 68.2 * 100;
				System.out.printf("%.2f", tmp);
				System.out.print("MB / 68.2MB (진행률: ");
				System.out.printf("%.1f", percent);
				System.out.println("%)");
				fos.write(buf, 0, (int) len);
			}
			fos.close();
			is.close();
			/*
			Runtime rt = Runtime.getRuntime();
			System.out.println("다운로드 완료\r\n폴더를 띄워드립니다.");
			rt.exec("explorer.exe C:\\Download\\");
			*/
        } catch (Exception e) {
            System.out.println("다운로드 오류입니다. 나중에 다시 받아보세요.");
        }
    }
}

'이것저것 배운것 > JAVA 공부중' 카테고리의 다른 글

JButton으로 종료시키기  (0) 2015.09.19
난수를 발생시키는 2가지 방법.  (0) 2015.07.27
Java GUI. 프레임을 화면 중앙에 출력하기  (0) 2015.06.02
JVM Option  (0) 2014.01.20
system.out.println  (0) 2013.12.31