헤르메스 LIFE

[Source] FTP Client Source 본문

Core Java

[Source] FTP Client Source

헤르메스의날개 2010. 7. 30. 23:54
728x90

Apach FTPClient 를 이용한 FTP Client Source

package com.study.springboot.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <pre>
 * Reply Codes
 *****************************************************************
 * 200 Command okay.
 * 500 Syntax error, command unrecognized.
 *     This may include errors such as command line too long.
 * 501 Syntax error in parameters or arguments.
 * 202 Command not implemented, superfluous at this site.
 * 502 Command not implemented.
 * 503 Bad sequence of commands.
 * 504 Command not implemented for that parameter.
 * 110 Restart marker reply.
 *     In this case, the text is exact and not left to the
 *     particular implementation; it must read:
 *          MARK yyyy = mmmm
 *     Where yyyy is User-process data stream marker, and mmmm
 *     server's equivalent marker (note the spaces between markers
 *     and "=").
 * 211 System status, or system help reply.
 * 212 Directory status.
 * 213 File status.
 * 214 Help message.
 *     On how to use the server or the meaning of a particular
 *     non-standard command.  This reply is useful only to the
 *     human user.
 * 215 NAME system type.
 *     Where NAME is an official system name from the list in the
 *     Assigned Numbers document.
 *  
 * 120 Service ready in nnn minutes.
 * 220 Service ready for new user.
 * 221 Service closing control connection.
 *     Logged out if appropriate.
 * 421 Service not available, closing control connection.
 *     This may be a reply to any command if the service knows it
 *     must shut down.
 * 125 Data connection already open; transfer starting.
 * 225 Data connection open; no transfer in progress.
 * 425 Can't open data connection.
 * 226 Closing data connection.
 *     Requested file action successful (for example, file
 *     transfer or file abort).
 * 426 Connection closed; transfer aborted.
 * 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
 *  
 * 230 User logged in, proceed.
 * 530 Not logged in.
 * 331 User name okay, need password.
 * 332 Need account for login.
 * 532 Need account for storing files.
 * </pre>
 */
public class FTPClientUtility {
    /**
     * Logger
     */
    private static final Logger logger      = LoggerFactory.getLogger(FTPClientUtility.class);
    private String              domainFile1 = null;
    private String              domainFile2 = null;
    private String              serverID    = null;
    private String              serverPW    = null;
    private String              serverIP    = null;

    public String getDomainFile1() {
        return domainFile1;
    }

    public void setDomainFile1( String domainFile1 ) {
        this.domainFile1 = domainFile1;
    }

    public String getDomainFile2() {
        return domainFile2;
    }

    public void setDomainFile2( String domainFile2 ) {
        this.domainFile2 = domainFile2;
    }

    public String getServerID() {
        return serverID;
    }

    public void setServerID( String serverID ) {
        this.serverID = serverID;
    }

    public String getServerPW() {
        return serverPW;
    }

    public void setServerPW( String serverPW ) {
        this.serverPW = serverPW;
    }

    public String getServerIP() {
        return serverIP;
    }

    public void setServerIP( String serverIP ) {
        this.serverIP = serverIP;
    }

    /**
     * 생성자
     */
    public FTPClientUtility() {
    }

    public String excute() throws Exception {
        FTPClient        client = null;
        FileInputStream  fis    = null;
        FileOutputStream fos    = null;
        String           rtnMsg = "-1";

        try {
            client = new FTPClient();
            client.connect(getServerIP());

            int replyCode = client.getReplyCode();
            logger.info("replyCode :: " + replyCode);

            if ( !FTPReply.isPositiveCompletion(replyCode) ) {
                client.disconnect();
                System.err.println("FTP server refused connection.");
            } else {
                boolean connect_stat = client.login(getServerID(), getServerPW());  // id, pw
                System.out.println("connect_stat :: " + connect_stat);
                System.out.println("WorkingDirectory :: " + client.printWorkingDirectory());

                // boolean rtnVal = client.changeWorkingDirectory("/wasapp/down/domain");
                boolean rtnVal = client.changeWorkingDirectory("경로");

                System.out.println("cd domain :: " + rtnVal + " WorkingDirectory :: " + client.printWorkingDirectory());

                client.enterLocalPassiveMode();  // Set the current data connection mode to
                                                 // PASSIVE_LOCAL_DATA_CONNECTION_MODE .

                System.out.println("\n" + client.getStatus());

                logger.info("=========================================================================");
                File uploadFile1 = new File(getDomainFile1());
                logger.info("파일여부 확인 :: " + uploadFile1.isFile());
                fis = new FileInputStream(uploadFile1);
                boolean isSuccess1 = client.storeFile(uploadFile1.getName(), fis);
                if ( isSuccess1 ) {
                    rtnMsg = "0";
                    System.out.println("파일 업로드 성공");
                } else {
                    rtnMsg = "-1";
                    System.out.println("파일 업로드 실패");
                }
                logger.info("=========================================================================");
                File uploadFile2 = new File(getDomainFile2());
                logger.info("파일여부 확인 :: " + uploadFile2.isFile());
                fis = new FileInputStream(uploadFile2);
                boolean isSuccess2 = client.storeFile(uploadFile2.getName(), fis);
                if ( isSuccess2 ) {
                    rtnMsg = "0";
                    System.out.println("파일 업로드 성공");
                } else {
                    rtnMsg = "-1";
                    System.out.println("파일 업로드 실패");
                }
                logger.info("=========================================================================");

                boolean chkout = client.logout();
                System.out.println("Logout status :: " + chkout);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if ( fis != null ) {
                try {
                    fis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if ( fos != null ) {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if ( client != null || client.isConnected() ) {
                try {
                    client.disconnect();
                    System.out.println("FTP Disconnected......... ");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return rtnMsg;
    }
}

 

728x90

'Core Java' 카테고리의 다른 글

[Source] Unicode 2.0 을 Unicode 1.2 로 변환  (0) 2010.10.20
[Source] Character Set Conversion  (0) 2010.10.20
[Source] String to Unicode  (0) 2010.10.20
[Source] Unicode To String  (0) 2010.10.20
[Source] CVS 폴더 삭제  (0) 2010.09.06