2013년 9월 2일 월요일

[오라클자바커뮤니티강좌]자바로 만든 FTP Client(파일업로드예)

FTP를 위한 컴포넌트는 http://www.savarese.org/downloads/NetComponents/ 에서 다운 받으시구요...

/**------------------------------------------------------------------------------------
  * 시스템명 : FTP Client
  * 작 성 자 :
  * 수 정 자 : 이종철
  * 파 일 명 : OracleJavaFtpClient.java
  * 버    전 : 1.3
  * 개    요 :파일을 FTP서버의 Log 폴더에 저장한다.
   *-----------------------------------------------------------------------------------*/


package new_OracleJava.util;

import java.io.*;
import new_OracleJava.engine.OracleJavaConfig;
import com.oroinc.net.ftp.*;


public class OracleJavaFtpClient{
    static String server = "xxx.xxx.xxx.xxx";
    static int port = 21;
    public static String id = "id"
    public static String password = "pwd"
   
    FTPClient ftpClient = null;

    public OracleJavaFtpClient() {     
            ftpClient = new FTPClient();
    }

    public void sendFile() {
           
            try {                                                 
                    boolean res = false;
            String MyFile = "/use/local/logs/a.log";         
               
            File f = new File(MyFile);

            BufferedInputStream bi = new BufferedInputStream(new FileInputStream(f.getPath()));
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
           
            //FTP 서버의 폴더...
            ftpClient.changeWorkingDirectory("/ftpserver/usr/local");
           
            if (bi != null) {
              res = ftpClient.storeFile(OracleJavaConfig.libSystemChkFileName, bi);
            }         
            else {
                    System.out.println("FileStrema 생성 오류!!!");
            }
           
            bi.close();
                       
            //ftpClient.storeFile(OracleJavaConfig.libWAS2FTPPath, new FileInputStream(new File(MyFile)));
            if (res) {
                    Log.println("OracleJavaSystemChkThread", "FTP 전송 OK~~~");
                    System.out.println("FTP 전송 OK~~~");
            }
            else {
                    Log.println("OracleJavaSystemChkThread", "FTP 전송 실패~~~");
                    System.out.println("FTP 전송 실패~~~");
            }
                //ftp.logout();
                //ftp.disconnect(); 
                //ftp = null;
            }
            catch(Exception e) {
                Log.println("OracleJavaSystemChkThread", "*******************************************************************************");
          Log.println("OracleJavaSystemChkThread", "******************************** Exception FTP Send****************************");
          Log.println("OracleJavaSystemChkThread", "*******************************************************************************");
          Log.println("OracleJavaSystemChkThread", e.toString() + "\n");
            }
    }

    // 계정과 패스워드로 로그인
    public boolean login(String user, String password) {
        try {
            //this.connect();
            return ftpClient.login(user, password);
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return false;
    }

    // 서버로부터 로그아웃
    public boolean logout() {
        try {
            return ftpClient.logout();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return false;
    }

    // 서버로 연결
    public void connect() {
        try {
           
            ftpClient.connect(server, port);           
            int reply;
            // 연결 시도후, 성공했는지 응답 코드 확인
            reply = ftpClient.getReplyCode();
       
            if(!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();               
                Log.println("OracleJavaSystemChkThread", "*************************************************");
                Log.println("OracleJavaSystemChkThread", "서버로부터 연결을 거부당했습니다\n");
                Log.println("OracleJavaSystemChkThread", "************************************************");
                System.exit(1);
            }
        }
        catch (IOException ioe) {
            if(ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch(IOException f) {
                    //
                }
            }
            Log.println("OracleJavaSystemChkThread", "*************************************************");
            Log.println("OracleJavaSystemChkThread", "서버 연결 불가!!\n");
            Log.println("OracleJavaSystemChkThread", "************************************************");           
        }

// FTP의 ls 명령, 모든 파일 리스트를 가져온다
    public FTPFile[] list() {
        FTPFile[] files = null;
        try {
            files = this.ftpClient.listFiles();
            return files;
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return null;
    }

    // 파일을 전송 받는다
    public File get(String source, String target) {
        OutputStream output = null;
        try {
            File local = new File(source);
            output = new FileOutputStream(local);
        }
        catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        }
        File file = new File(source);
        try {
            if (ftpClient.retrieveFile(source, output)) {
                return file;
            }
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return null;
    }


    }

    // 서버로부터 연결을 닫는다
    public void disconnect() {
        try {
            ftpClient.disconnect();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

}




=======================================================
=======================================================

다른 자바 프로그램에서... 다음과 같이 호출하여 이용했습니다.

                OracleJavaFtpClient ftpClient = new OracleJavaFtpClient();               
                ftpClient.connect();
                ftpClient.login(OracleJavaFtpClient.id, OracleJavaFtpClient.password);
                ftpClient.sendFile();
                ftpClient.logout();
                ftpClient.disconnect();

[출처] 오라클자바커뮤니티 - http://www.oraclejavanew.kr/bbs/board.php?bo_table=LecJavaNet&wr_id=21

오라클자바커뮤니티 실전강좌



댓글 없음:

댓글 쓰기