투케이2K

850. (Android/Java) [commons-net-3.6.jar] FTP 서버 연결 및 파일 업로드 수행 - storeFile 본문

Android

850. (Android/Java) [commons-net-3.6.jar] FTP 서버 연결 및 파일 업로드 수행 - storeFile

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

[개발 환경 설정]

개발 툴 : 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 boolean upLoadFile(String originPath, String ftpPath, String ftpFileName) {
        S_Log._D_("FTP 서버 파일 업로드 수행", new String[]{ "originPath :: " + String.valueOf(originPath), "ftpPath :: " + String.valueOf(ftpPath), "ftpFileName :: " + String.valueOf(ftpFileName) });

        // [리턴 변수 선언]
        boolean result = false;

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

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

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

                    if (C_Util.stringNotNull(originPath) == true && C_Util.stringNotNull(ftpPath) == true && C_Util.stringNotNull(ftpFileName) == true){

                        // [해당 경로 파일 스트림 읽음]
                        File originFile = new File(originPath);

                        if (originFile.exists() == true){

                            if (ftpClient.changeWorkingDirectory(ftpPath) == true){

                                FileInputStream fis = new FileInputStream(originFile);

                                result = ftpClient.storeFile(ftpFileName, fis); // [파일 업로드 수행]

                                fis.close();

                                if (result == true){
                                    S_Log._W_("FTP 서버 파일 업로드 성공", new String[]{ "FTP Path :: " + String.valueOf(ftpPath), "FTP File Name :: " + String.valueOf(ftpFileName) });
                                }
                                else {
                                    S_Log._E_("FTP 서버 파일 업로드 실패", new String[]{ "FTP Path :: " + String.valueOf(ftpPath), "FTP File Name :: " + String.valueOf(ftpFileName) });
                                }
                            }
                            else {
                                S_Log._E_("FTP 서버 파일 업로드 에러 발생", new String[]{ "Error :: ftpClient.changeWorkingDirectory == false" });
                            }

                        }
                        else {
                            S_Log._E_("FTP 서버 파일 업로드 에러 발생", new String[]{ "Error :: originFile Path Is Null" });
                        }

                    }
                    else {
                        S_Log._E_("FTP 서버 파일 업로드 에러 발생", new String[]{ "Error :: File Path 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 result;
    }
 

반응형
Comments