달력

42024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

자바 url 파일 받기

자바 2012. 8. 22. 15:14

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 test {

/**

* 버퍼 사이즈

*/

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);

}

}


출처 : http://forum.falinux.com/zbxe/?document_srl=565194


'자바' 카테고리의 다른 글

Eclipse 배경색 background 복구 색 변경 default  (0) 2014.05.02
Gson 받기 사용 방법 두 가지  (0) 2013.06.05
gwt may need recompiled  (0) 2013.02.13
Posted by dewlit
|