본문 바로가기

AWS CLOUD FRAMEWORK/Java

[Day17] Ex05

package file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;


public class Ex05 {
	public static void main(String[] args) throws Exception {
		
		InputStream is = null;
		FileOutputStream fos = null;
		
		byte[] buf = new byte[1024];
		int i = 0;
		int total = 0;
		
		Scanner sc = new Scanner(System.in);
		System.out.print("자원위치 입력 : ");
		String resource = sc.nextLine();
		
		// Uniform Resource Locator : 자원의 위치를 알리는 문자열 형식
		URL url = new URL(resource);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		String contentType = conn.getContentType();	// 자원의 유형
		int size = conn.getContentLength();			// 자원의 전체 크기 (byte)
		
		System.out.printf("%s : %d byte\n", contentType, size);
		
		String fileName = "ex05." + contentType.split("/")[1];	// 새로 만들 파일이름
		
		File dest = new File(fileName);				
		dest.createNewFile();
		
		is = url.openStream();
		fos = new FileOutputStream(dest);
		
		// 파일의 내용을 buf에 담는다. 0은 간격을 띄우는 크기. 1024는 버퍼의 크기
		while((i = is.read(buf, 0, 1024)) != -1) {	// -1 : EOF (End Of File)
			// buf에 담긴 내용을 새 파일에 기록한다. 여백 0, 
			fos.write(buf, 0, i);	// i만큼만 기록한다. 1024만큼 기록하지 않고, 읽어낸 크기만큼만 기록
			fos.flush();
			total += i;
			System.out.printf("%d / %d (%3d %%)\n", total, size, total * 100 / size);
		}
		System.out.println(dest.getName());
		
		fos.flush();
		is.close();
		fos.close();
		sc.close();
		
	}
}

'AWS CLOUD FRAMEWORK > Java' 카테고리의 다른 글

[Day17] Ex07  (0) 2023.04.06
[Day17] Ex06  (0) 2023.04.06
[Day17] Ex04  (0) 2023.04.06
[Day17] Ex03  (0) 2023.04.06
[Day17] Ex02  (0) 2023.04.06