Java - URL 경로의 파일 다운로드및 서비스 활용
Posted 08 31, 2013 16:45, Filed under: OpenSource# URL 경로의 파일을 다운로드, 서비스 활용
> 용도
- URL 위치의 파일(웹페이지 포함)을 특정 위치에 다운로드
(파일을 다운로드 하고 원하는 위치에 업로드 하는 작업 축소)
> 활용
- 다운로드된 파일을 즉시 웹(블로그)으로 서비스 하고자 할때.
- DBMS 저장, 웹 또는 개인 블로그에서 외부 공개및 비공개로 활용
- 디렉토리명을 일자별 변경하여 파일 그룹 관리
> 사용 라이브러리
Apache Tika
FileUrlDownload.java
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import java.net.URLDecoder; import org.apache.tika.Tika; import bean.FileBean; /** * # URL상의 파일을 다운로드, DBMS에 저장 */ public class FileUrlDownload { final static int bufferSize = 1024; /** * # URL 경로의 파일 다운로드 */ public static int fileUrlReadAndDownload(String fileUrl, String localFileName, String downloadDir) { OutputStream outStream = null; URLConnection uCon = null; InputStream is = null; int byteWritten = 0; try { URL Url; byte[] buf; int byteRead; Url = new URL(fileUrl); outStream = new BufferedOutputStream( new FileOutputStream(downloadDir + File.separator + URLDecoder.decode( localFileName, "UTF-8"))); uCon = Url.openConnection(); is = uCon.getInputStream(); buf = new byte[bufferSize]; 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(); } } return byteWritten; } /** * # 다운로드된 파일 정보를 Bean 클래스에 저장 * - DBMS에 저장및 정보 활용을 위함. */ public static FileBean setUrlFileSave(String fileUrl, String downloadDir) { FileBean fileBean = new FileBean(); int fileSize = 0; int slashIndex = fileUrl.lastIndexOf('/'); int periodIndex = fileUrl.lastIndexOf('.'); // 파일 경로에서 마지막 파일명을 추출 String fileName = fileUrl.substring(slashIndex + 1); String filePath = downloadDir+File.separator+fileName; if (periodIndex >= 1 && slashIndex >= 0 && slashIndex < fileUrl.length() - 1) { fileSize = fileUrlReadAndDownload(fileUrl, fileName, downloadDir); } fileBean.setFileName(fileName); fileBean.setFilePath(filePath); fileBean.setFileSize(fileSize); String mimeType = null; Tika tika = new Tika(); // 파일의 Mime-Type 추출 try { mimeType = tika.detect(new File(filePath)); } catch (IOException e) { e.printStackTrace(); } fileBean.setFileType(mimeType); // 파일 Mime-Type return fileBean; } /** * # main */ public static void main(String[] args) { // 파일 URL String fileUrl = ""; // 다운로드 디렉토리 String downDir = "data"; /* * 파일 다운로드및 파일 정보를 저장하여 서비스 활용 */ setUrlFileSave(fileUrl, downDir); } }
FileBean.java
/** * # 파일 정보를 담을 Bean 클래스 */ public class FileBean { private int fileSeq; // 파일 번호 private String fileName; // 파일 명 private String fileMemo; // 파일 정보 private String fileType; // 파일 타입 private int fileSize; // 파일 사이즈 private String filePath; // 업로드 된 파일 경로 private String filePathOrg; // 실제 파일 경로 : URL 정보 private int isOpen; // 공개여부 private String createDate; // 생성일자 private String createId; // 생성자 아이디 public String toString(){ StringBuffer sb = new StringBuffer(); sb.append("### FileBean.toString() ###"); sb.append(" fileSeq :"+ this.fileSeq); sb.append(" fileName :"+ this.fileName); sb.append(" fileMemo :"+ this.fileMemo); sb.append(" fileType :"+ this.fileType); sb.append(" fileSize :"+ this.fileSize); sb.append(" filePath :"+ this.filePath); sb.append(" filePathOrg :"+ this.filePathOrg); sb.append(" isOpen :"+ this.isOpen); sb.append(" createDate :"+ this.createDate); sb.append(" createId :"+ this.createId); return sb.toString(); } public String getFilePathOrg() { return filePathOrg; } public void setFilePathOrg(String filePathOrg) { this.filePathOrg = filePathOrg; } public String getCreateId() { return createId; } public void setCreateId(String createId) { this.createId = createId; } public int getFileSeq() { return fileSeq; } public void setFileSeq(int fileSeq) { this.fileSeq = fileSeq; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFileMemo() { return fileMemo; } public void setFileMemo(String fileMemo) { this.fileMemo = fileMemo; } public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType = fileType; } public int getFileSize() { return fileSize; } public void setFileSize(int fileSize) { this.fileSize = fileSize; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public int getIsOpen() { return isOpen; } public void setIsOpen(int isOpen) { this.isOpen = isOpen; } public String getCreateDate() { return createDate; } public void setCreateDate(String createDate) { this.createDate = createDate; } }
※ 위 내용은, 여러 자료를 참고하거나 제가 주관적으로 정리한 것입니다.
잘못된 정보나 보완이 필요한 부분을, 댓글 또는 메일로 보내주시면 많은 도움이 되겠습니다.
잘못된 정보나 보완이 필요한 부분을, 댓글 또는 메일로 보내주시면 많은 도움이 되겠습니다.
"OpenSource" 분류의 다른 글
Linux - 개인 웹 방화벽 구축(iptables, j2ssh 이용) (0) | 2013/02/03 |
Java - MetaData를 활용하여 Database의 테이블별 데이타를 파일로 저장 (0) | 2009/11/05 |
Java - Commons Net의 FTPClient 사용하여 FTP 접속및 파일 컨트롤 완성 소스 (0) | 2009/09/01 |
Trackback URL : http://develop.sunshiny.co.kr/trackback/950