투케이2K

849. (Android/Java) [commons-net-3.6.jar] FTP 서버 연결 및 파일 리스트 확인 - listFiles 본문

Android

849. (Android/Java) [commons-net-3.6.jar] FTP 서버 연결 및 파일 리스트 확인 - listFiles

투케이2K 2024. 8. 26. 17:31
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java / Kotlin

 

[소스 코드]

    // ------------------------------------------------------------------------------------------
    // TODO [전역 변수 선언 실시]
    // ------------------------------------------------------------------------------------------
    String userName = "twok123";
    String host = "towk.test.co.kr";
    int port = 21; // [FTP 포트]
    String password = "twok0123456789";

    private Context mMainCtx; // [컨텍스트]

    boolean isConnection = false; // [연결 상태 체크 변수]

    FTPClient ftpClient = null; // [FTPClient]






    // ------------------------------------------------------------------------------------------
    // TODO [FTP 서버 연결 실시]
    // ------------------------------------------------------------------------------------------
    public synchronized boolean connectServer(String userName, String host, int port, String password) {
        S_Log._D_("FTP 서버 연결 수행", new String[]{
                "userName :: " + String.valueOf(userName),
                "host :: " + String.valueOf(host),
                "port :: " + String.valueOf(port),
                "pw :: " + String.valueOf(password)
        });


        // [변수 초기화]
        isConnection = false;


        // [로직 처리 수행]
        try {

            if (mMainCtx != null && ftpClient == null){

                if (C_Util.stringNotNullMulti(new String[]{userName, host, String.valueOf(port), password}) == true){

                    ftpClient = new FTPClient();

                    ftpClient.connect(host, port); // [호스트 연결 수행]

                    if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){

                        ftpClient.login(userName, password); // [호스트 로그인 수행]

                        isConnection = ftpClient.isConnected(); // [변수에 연결 상태 값 저장 수행]

                    }
                    else {
                        S_Log._E_("FTP 서버 연결 수행 에러 발생", new String[]{ "Error :: isPositiveCompletion False" });

                        try {
                            if (ftpClient != null){
                                ftpClient.disconnect();
                            }
                            ftpClient = null;
                        }
                        catch (Exception es){}
                    }
                }
                else {
                    S_Log._E_("FTP 서버 연결 수행 에러 발생", new String[]{ "Error :: Input Data Is Null" });
                }

            }
            else {
                S_Log._E_("FTP 서버 연결 수행 에러 발생", new String[]{ "Error :: mMainCtx Is Null or ftpClient Not Null" });
            }

        }
        catch (Exception e){
            e.printStackTrace();
        }


        // [로그 출력 수행]
        S_Log._W_("FTP 서버 연결 수행 결과 확인", new String[]{
                "RETURN :: " + String.valueOf(isConnection)
        });


        // [리턴 반환 수행]
        return isConnection;
    }





    // ------------------------------------------------------------------------------------------
    // TODO [FTP 서버 파일 목록 확인]
    // ------------------------------------------------------------------------------------------
    public synchronized FTPFile[] getFtpFileList(String path) {
        S_Log._D_("FTP 서버 파일 목록 확인 수행", new String[]{ "path :: " + String.valueOf(path) });

        // [리턴 변수 선언]
        FTPFile files[] = null;

        // [로직 처리 수행]
        try {

            if (mMainCtx != null && ftpClient != null && isConnection == true){

                if (ftpClient.isConnected() == true){

                    if (C_Util.stringNotNull(path) == true){
                        files = ftpClient.listFiles(path); // [특정 경로 지정] : [/html]
                    }
                    else {
                        files = ftpClient.listFiles(); // [전체 루트 경로]
                    }

                    if (files != null && files.length > 0){
                        S_Log._W_("FTP 서버 파일 목록 확인 수행", new String[]{ Arrays.toString(files) });
                    }
                    else {
                        S_Log._E_("FTP 서버 파일 목록 확인 에러 발생", new String[]{ "Error :: listFiles Is Null" });
                    }

                }
                else {
                    S_Log._E_("FTP 서버 파일 목록 확인 에러 발생", new String[]{ "Error :: ftpClient.isConnected() == false" });
                }

            }
            else {
                S_Log._E_("FTP 서버 파일 목록 확인 에러 발생", new String[]{ "Error :: mMainCtx Is Null or ftpClient Not Null or isConnection false" });
            }

        }
        catch (Exception e){
            e.printStackTrace();
        }

        // [리턴 결과 반환]
        return files;
    }
 
 

 

반응형
Comments