1. 다운로드 할 파일 주소에서 파일 이름 취득.

2. 파일 주소로 접속해 파일을 읽어들임.

3. 읽어들인 파일을 다운로드 디렉토리에 1.에서 취득한 파일 이름으로 쓰기.

4. 종료.

 

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class FileUrlDownload {
 /**
  * 버퍼 사이즈
  */
 final static int size = 1024;

 /**
  * fileAddress에서 파일을 읽어, 다운로드 디렉토리에 다운로드
  * 
  * @param fileAddress
  * @param localFileName
  * @param downloadDir
  */
 public static void fileUrlReadAndDownload(String fileAddress,
   String localFileName, String downloadDir) {
  OutputStream outStream = null;
  URLConnection uCon = null;

  InputStream is = null;
  try {

   System.out.println("-------Download Start------");

   URL Url;
   byte[] buf;
   int byteRead;
   int byteWritten = 0;
   Url = new URL("fileAddress);
   outStream = new BufferedOutputStream(new FileOutputStream(
     downloadDir + "\\" + localFileName));

   uCon = Url.openConnection();
   is = uCon.getInputStream();
   buf = new byte[size];
   while ((byteRead = is.read(buf)) != -1) {
    outStream.write(buf, 0, byteRead);
    byteWritten += byteRead;
   }

   System.out.println("Download Successfully.");
   System.out.println("File name : " + localFileName);
   System.out.println("of bytes  : " + byteWritten);
   System.out.println("-------Download End--------");

  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    is.close();
    outStream.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 /**
  * 
  * @param fileAddress
  * @param downloadDir
  */
 public static void fileUrlDownload(String fileAddress, String downloadDir) {

  int slashIndex = fileAddress.lastIndexOf('/');
  int periodIndex = fileAddress.lastIndexOf('.');

  // 파일 어드레스에서 마지막에 있는 파일이름을 취득
  String fileName = fileAddress.substring(slashIndex + 1);

  if (periodIndex >= 1 && slashIndex >= 0
    && slashIndex < fileAddress.length() - 1) {
   fileUrlReadAndDownload(fileAddress, fileName, downloadDir);
  } else {
   System.err.println("path or file name NG.");
  }
 }

 /**
  * main
  * 
  * @param args
  */
 public static void main(String[] args) {

  // 파일 어드레스
  String url = "http://localhost/download/index.php";
  // 다운로드 디렉토리
  String downDir = "C:/Temp";

  // 다운로드 호출
  fileUrlDownload(url, downDir);

 }
}

 

저는 localhost/download/index.php란 파일을 다운로드하는 샘플 소스입니다.

실행하면 아래와 같이 출력됩니다.

-------Download Start------
Download Successfully.
File name : index.php
of bytes  : 2605
-------Download End--------



Posted by 사라링
BLOG main image
.. by 사라링

카테고리

사라링님의 노트 (301)
JSP (31)
J-Query (41)
JAVA (24)
VM-WARE (0)
디자인패턴 (1)
스크랩 (0)
스트러츠 (3)
안드로이드 (11)
오라클 (45)
우분투-오라클 (1)
이클립스메뉴얼 (6)
스프링3.0 (23)
자바스크립트 (10)
HTML5.0 (17)
정보처리기사 (1)
기타(컴퓨터 관련) (1)
문제점 해결 (3)
프로젝트 (2)
AJAX (4)
하이버네이트 (3)
트러스트폼 (11)
Jeus (2)
재무관리(회계) (5)
정규식 (5)
아이바티스 (8)
취미 (2)
소프트웨어 보안 관련모음 (0)
정보보안기사 (6)
C언어 베이직 및 프로그램 (3)
보안 관련 용어 정리 (2)
넥사크로 (6)
Total :
Today : Yesterday :