투케이2K

134. (TWOK/UTIL) [Android/Java] C_SFTP_Client_Module : SFTP 클라이언트 유틸 파일 본문

투케이2K 유틸파일

134. (TWOK/UTIL) [Android/Java] C_SFTP_Client_Module : SFTP 클라이언트 유틸 파일

투케이2K 2024. 10. 2. 20:41

[설 명]

프로그램 : Android / Java

설 명 : C_SFTP_Client_Module : SFTP 클라이언트 유틸 파일

 

[소스 코드]

 

package com.example.javaproject.C_Module;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.example.javaproject.C_Util;
import com.example.javaproject.S_Log;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpProgressMonitor;

import org.apache.commons.net.ftp.FTP;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Vector;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class C_SFTP_Client_Module {


    /**
     * // --------------------------------------------------------------------------------------
     * TODO [클래스 설명]
     * // --------------------------------------------------------------------------------------
     * 1. SFTP 파일 전송 클라이언트 클래스 [SFTP 구축 및 테스트 필요 클래스]
     * // --------------------------------------------------------------------------------------
     * 2. 필요 라이브러리 :
     *
     * // 라이브러리 버전 참고 : https://mvnrepository.com/artifact/com.jcraft/jsch
     * implementation group: 'com.jcraft', name: 'jsch', version: '0.1.54'
     * // --------------------------------------------------------------------------------------
     * */





    /**
     * // --------------------------------------------------------------------------------------
     * TODO [빠른 로직 찾기 : 주석 로직 찾기]
     * // --------------------------------------------------------------------------------------
     *
     * // --------------------------------------------------------------------------------------
     *
     * // --------------------------------------------------------------------------------------
     *
     * // --------------------------------------------------------------------------------------
     */





    // ------------------------------------------------------------------------------------------
    // TODO [사용 방법]
    // ------------------------------------------------------------------------------------------
    /*
    try {

        new Thread(){
            @Override
            public void run(){
                try {

                    // -------------------------------------------------
                    // TODO [1]. [SFTP 서버 접속을 위한 변수 선언]
                    // -------------------------------------------------
                    String userName = "root";
                    String host = "125.78.167.135";
                    int port = 22;
                    String password = "admin1234";


                    // --------------------------------------
                    // TODO [2]. [SFTP 클라이언트 클래스 인스턴스 초기화]
                    // --------------------------------------
                    C_SFTP_Client_Module c_sftp_client_module = C_SFTP_Client_Module.getInstance();
                    c_sftp_client_module.setContext(A_Test.this);


                    // --------------------------------------
                    // TODO [3]. [SFTP 서버 연결 수행]
                    // --------------------------------------
                    if(c_sftp_client_module.connectServer(userName, host, port, password) == true) { // [서버 연결 상태 확인]

                        // -------------------------------------------
                        // TODO [파일 업로드 수행]
                        // -------------------------------------------
                        //String originPath = C_App.innerFilePath(A_Test.this) + "/LOG_FILE_FOLDER/APP_CRASH_LOG_FILE.txt";

                        //String sftpPath = "/test";
                        //String sftpFileName = "log.txt";

                        //boolean result = c_sftp_client_module.uploadFile(originPath, sftpPath, sftpFileName);

                        //S_Log._W_("SFTP 파일 업로드 결과 확인", new String[]{String.valueOf(result)});


                        // -------------------------------------------
                        // TODO [파일 다운 로드 수행]
                        // -------------------------------------------
                        //String appPath = C_App.innerFilePath(A_Test.this) + "/LOG_FILE_FOLDER";
                        //String appFileName = "download_log.txt";

                        //String sftpPath = "/test";
                        //String sftpFileName = "log.txt";

                        //boolean result = c_sftp_client_module.downLoadFile(appPath, appFileName, sftpPath, sftpFileName);

                        //S_Log._W_("SFTP 파일 다운 로드 결과 확인", new String[]{String.valueOf(result)});


                        // -------------------------------------------
                        // TODO [파일 리스트 확인 수행]
                        // -------------------------------------------
                        //String sftpPath = "/test";

                        //c_sftp_client_module.getSftpFileList(sftpPath);


                        // -------------------------------------------
                        // TODO [특정 파일 포함 확인 수행]
                        // -------------------------------------------
                        //String sftpPath = "/test";
                        //String fileName = "log.txt";

                        //c_sftp_client_module.getSftpFileExists(sftpPath, fileName);


                        // -------------------------------------------
                        // TODO [특정 파일 명칭 변경 수행]
                        // -------------------------------------------
                        //String oldSftpPath = "/test/log.txt";
                        //String newSftpPath = "/test/log2.txt";

                        //boolean result = c_sftp_client_module.renameFile(oldSftpPath, newSftpPath);

                        //S_Log._W_("SFTP 파일 명칭 변경 결과 확인", new String[]{String.valueOf(result)});


                        // -------------------------------------------
                        // TODO [파일 삭제 수행]
                        // -------------------------------------------
                        //String sftpPath = "/test";
                        //String sftpFileName = "log.txt";

                        //boolean result = c_sftp_client_module.deleteFile(sftpPath, sftpFileName);

                        //S_Log._W_("SFTP 파일 삭제 결과 확인", new String[]{String.valueOf(result)});

                    }
                    else {
                        S_Log._E_("SFTP 서버 연결 실패", null);
                    }

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

    }
    catch (Exception e) {
        e.printStackTrace();
    }
    */
    // ------------------------------------------------------------------------------------------





    // ------------------------------------------------------------------------------------------
    // TODO [전역 변수 선언]
    // ------------------------------------------------------------------------------------------
    private String ACTIVITY_NAME = "C_SFTP_Client_Module";

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

    boolean isConnection = false; // [연결 상태 체크 변수]
    boolean isSend = false; // [파일 전송 상태 체크 변수]
    private Lock lock = new ReentrantLock();

    ChannelSftp channelSftp = null; // [SFTP 연결 관리]
    Session session = null; // [SFTP 접속 관리]





    // ------------------------------------------------------------------------------------------
    // TODO [콘텍스트 지정]
    // ------------------------------------------------------------------------------------------
    public void setContext(Context ctx) {
        mMainCtx = ctx;
    }





    // ------------------------------------------------------------------------------------------
    // TODO [인스턴스 생성]
    // ------------------------------------------------------------------------------------------
    public static C_SFTP_Client_Module getInstance() { return C_SFTP_Client_Module.LazyHolder.INSTANCE; }
    private static class LazyHolder {
        private static final C_SFTP_Client_Module INSTANCE = new C_SFTP_Client_Module();
    }





    // ------------------------------------------------------------------------------------------
    // TODO [SFTP 서버 연결 실시]
    // ------------------------------------------------------------------------------------------

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


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


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

            // [JSch 객체 선언]
            JSch jSch = new JSch();

            if (session != null && session.isConnected() == true){ // [접속 관리]
                session.disconnect();
                session = null;
            }

            if (channelSftp != null && channelSftp.isConnected() == true){ // [연결 관리]
                channelSftp.disconnect();
                channelSftp = null;
            }


            // [파라이터 값으로 새로운 연결 세션 생성]
            session = jSch.getSession(String.valueOf(userName), String.valueOf(host), port);
            session.setPassword(String.valueOf(password));


            // [연결 생성 셋팅 값 설정]
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no"); // [처음 접속하는 호스트 yes/no 이슈 해결]
            config.put("PreferredAuthentications", "password"); // [ssh 접속 시 password 사용 설정]
            //config.put("kex", "diffie-hellman-group1-sha1"); // [IO Exception 대비 ㅣ 0.1.53 버전 이상]
            //config.put("kex", "diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256");
            session.setConfig(config); // [환경 설정]

            session.setTimeout(20000); // [세션 타임 아웃 시간]
            session.setServerAliveInterval(60000); // [서버 주기 체크 시간]


            // [sftp 연결 수행]
            //session.connect(20000); // [연결 수행 및 연결 타임 아웃 시간 지정]
            session.connect();
            Channel channel = session.openChannel("sftp");
            channel.connect();

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

                channelSftp = (ChannelSftp) channel;
                isConnection = true;

            }

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


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


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





    // ------------------------------------------------------------------------------------------
    // TODO [SFTP 서버 연결 종료]
    // ------------------------------------------------------------------------------------------
    public synchronized void disconnectServer() {
        S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 연결 종료", null);


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


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

            if (channelSftp != null && channelSftp.isConnected() == true){
                channelSftp.disconnect();
                channelSftp = null;
            }

            if (session != null && session.isConnected() == true){
                session.disconnect();
                session = null;
            }

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

    }





    // ------------------------------------------------------------------------------------------
    // TODO [SFTP 파일 전송 수행 (클라이언트 (앱) >> 서버)]
    // ------------------------------------------------------------------------------------------
    // TODO Param : serverDir : /
    // ------------------------------------------------------------------------------------------
    // TODO Param : clientFilePath : Environment.getExternalStorageDirectory().getAbsolutePath() + "/Files/log.txt";
    // ------------------------------------------------------------------------------------------
    public synchronized boolean uploadFile(String originPath, String sftpPath, String sftpFileName) {
        S_Log._D_(ACTIVITY_NAME + " :: SFTP 파일 전송 경로 확인", new String[]{
                "originPath :: " + String.valueOf(originPath),
                "ftpPath :: " + String.valueOf(sftpPath),
                "ftpFileName :: " + String.valueOf(sftpFileName)
        });


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


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

            if (channelSftp != null && channelSftp.isConnected() == true){

                if (C_Util.stringNotNullMulti(new String[]{originPath, sftpPath, sftpFileName}) == true){

                    // [클라이언트 파일 읽기]
                    File file = new File(originPath);

                    if (file.exists() == true){ // [해당 파일이 존재하는 경우]

                        //*
                        String serverDir = sftpPath;
                        if (serverDir.endsWith("/") == true){

                            serverDir += sftpFileName;
                        }
                        else {
                            serverDir += "/" + sftpFileName;
                        }

                        channelSftp.put(originPath, serverDir, new SftpProgressMonitor() {

                            private long FileSize = 0; // [전체 파일 사이즈]
                            private long sendFileSize = 0; // [실시간 전송 파일 사이즈]

                            @Override
                            public void init(int op, String src, String dest, long max) {
                                S_Log._D_(ACTIVITY_NAME + " :: SFTP [초기] 파일 전송 정보 확인", new String[]{
                                        "src [원본] :: " + String.valueOf(src),
                                        "dest [목적지] :: " + String.valueOf(dest)
                                });

                                // [변수에 지정 수행]
                                FileSize = max;
                            }

                            @Override
                            public boolean count(long count) {

                                // -----------------------------------------------------
                                // TODO [java.io.IOException: Pipe closed 발생 시 참고]
                                // -----------------------------------------------------
                                // build.gradle 라이브러리 의존성 버전 변경 [0.1.42]
                                // -----------------------------------------------------

                                if (FileSize != 0 && FileSize > 0){

                                    sendFileSize += count;
                                    long progress = sendFileSize * 100 / FileSize;

                                    if ((int) progress != 0){
                                        S_Log._D_(ACTIVITY_NAME + " :: SFTP [실시간] 파일 전송 정보 확인", new String[]{
                                                "progress :: " + String.valueOf(progress)
                                        });
                                    }

                                }

                                return true;
                            }

                            @Override
                            public void end() {
                                S_Log._W_(ACTIVITY_NAME + " :: SFTP [파일 전송] 완료 확인", null);

                                // [변수 삽입]
                                isSend = true;
                            }
                        });
                        // */

                    }
                    else {
                        S_Log._E_(ACTIVITY_NAME + " :: SFTP [파일 전송] 에러 확인", new String[]{ "ERROR :: 클라이언트에 저장된 파일 확인 필요 (file not exists)" });
                    }

                }
                else {
                    S_Log._E_(ACTIVITY_NAME + " :: SFTP [파일 전송] 에러 확인", new String[]{ "ERROR :: Input Data Is Null" });
                }

            }

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


        // [로그 출력 수행]
        S_Log._W_(ACTIVITY_NAME + " :: SFTP 파일 전송 결과 확인", new String[]{ "RETURN :: " + String.valueOf(isSend) });


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





    // ------------------------------------------------------------------------------------------
    // TODO [SFTP 서버 파일 다운 로드 수행]
    // ------------------------------------------------------------------------------------------
    public synchronized boolean downLoadFile(String appPath, String appFileName, String sftpPath, String sftpFileName) {
        S_Log._D_(ACTIVITY_NAME + " :: SFTP 서버 파일 다운 로드 수행", new String[]{ "appPath :: " + String.valueOf(appPath), "appFileName :: " + String.valueOf(appFileName), "sftpPath :: " + String.valueOf(sftpPath), "sftpFileName :: " + String.valueOf(sftpFileName) });

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

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

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

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

                    if (C_Util.stringNotNull(appPath) == true && C_Util.stringNotNull(appFileName) == true && C_Util.stringNotNull(sftpPath) == true && C_Util.stringNotNull(sftpFileName) == true){

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

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

                            String appSrcPath = appPath;
                            if (appPath.endsWith("/") == true){
                                appSrcPath += appFileName;
                            }
                            else {
                                appSrcPath += "/" + appFileName;
                            }

                            // [해당 원격 경로 이동 수행]
                            channelSftp.cd(sftpPath);

                            // [저장 된 파일 목록 확인]
                            Vector<ChannelSftp.LsEntry> fileAndFolderList = channelSftp.ls(sftpPath);

                            S_Log._W_(ACTIVITY_NAME + " :: SFTP 서버 파일 리스트 확인", new String[]{String.valueOf(fileAndFolderList)});

                            if (String.valueOf(fileAndFolderList).contains(sftpFileName) == true){

                                // [원격 파일 경로 확인]
                                String sftpPathFile = sftpPath;
                                if (sftpPath.endsWith("/") == true){
                                    sftpPathFile += sftpFileName;
                                }
                                else {
                                    sftpPathFile += "/" + sftpFileName;
                                }

                                // [SFTP 서버 파일 다운 로드 수행]
                                channelSftp.get(sftpPathFile, appSrcPath);

                                result = true; // [리턴 변수 삽입]

                                S_Log._W_(ACTIVITY_NAME + " :: SFTP 서버 파일 다운 로드 수행 성공", new String[]{ "SFTP Path :: " + String.valueOf(sftpPathFile), "APP Path :: " + String.valueOf(appSrcPath) });

                            }
                            else {
                                S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 다운 로드 에러 발생", new String[]{ "Error :: Sftp File List Not Found" });
                            }

                        }
                        else {
                            S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 다운 로드 에러 발생", new String[]{ "Error :: appFile Path Is Null" });
                        }

                    }
                    else {
                        S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 다운 로드 에러 발생", new String[]{ "Error :: Input Data Is Null" });
                    }

                }
                else {
                    S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 다운 로드 에러 발생", new String[]{ "Error :: channelSftp.isConnected() == false" });
                }

            }
            else {
                S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 다운 로드 에러 발생", new String[]{ "Error :: mMainCtx Is Null or channelSftp Not Null or isConnection false" });
            }

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

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





    // ------------------------------------------------------------------------------------------
    // TODO [SFTP 서버 파일 목록 확인 수행]
    // ------------------------------------------------------------------------------------------
    public synchronized ArrayList getSftpFileList(String sftpPath) {
        S_Log._D_(ACTIVITY_NAME + " :: SFTP 서버 파일 목록 확인 수행", new String[]{ "sftpPath :: " + String.valueOf(sftpPath) });

        // [리턴 변수 선언]
        ArrayList result = new ArrayList();


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

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

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

                    if (C_Util.stringNotNull(sftpPath) == true){

                        // [해당 원격 경로 이동 수행]
                        channelSftp.cd(sftpPath);

                        // [저장 된 파일 목록 확인]
                        Vector<ChannelSftp.LsEntry> fileAndFolderList = channelSftp.ls(sftpPath);

                        if (fileAndFolderList != null && fileAndFolderList.size()>0 && fileAndFolderList.isEmpty() == false){

                            for (int i=0; i<fileAndFolderList.size(); i++){
                                if (fileAndFolderList.get(i).getFilename().trim().replaceAll("\\.", "").length() > 0){
                                    result.add(fileAndFolderList.get(i).getFilename());
                                }
                            }

                            S_Log._W_(ACTIVITY_NAME + " :: SFTP 서버 파일 리스트 확인 성공", new String[]{String.valueOf(result)});
                        }
                        else {
                            S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 목록 확인 에러 발생", new String[]{ "Error :: channelSftp.ls Is Null" });
                        }

                    }
                    else {
                        S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 목록 확인 에러 발생", new String[]{ "Error :: Input Data Is Null" });
                    }

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

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

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

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





    // ------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------
    // TODO [SFTP 서버 파일 존재 확인 수행]
    // ------------------------------------------------------------------------------------------
    public synchronized boolean getSftpFileExists(String sftpPath, String fileName) {
        S_Log._D_(ACTIVITY_NAME + " :: SFTP 서버 파일 존재 확인 수행", new String[]{ "sftpPath :: " + String.valueOf(sftpPath), "fileName :: " + String.valueOf(fileName) });

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


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

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

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

                    if (C_Util.stringNotNull(sftpPath) == true){

                        // [해당 원격 경로 이동 수행]
                        channelSftp.cd(sftpPath);

                        // [저장 된 파일 목록 확인]
                        Vector<ChannelSftp.LsEntry> fileAndFolderList = channelSftp.ls(sftpPath);

                        if (fileAndFolderList != null && fileAndFolderList.size()>0 && fileAndFolderList.isEmpty() == false){

                            for (int i=0; i<fileAndFolderList.size(); i++){
                                if (String.valueOf(fileAndFolderList.get(i).getFilename()).equals(fileName) == true){
                                    result = true;
                                    break;
                                }
                            }

                            S_Log._W_(ACTIVITY_NAME + " :: SFTP 서버 파일 존재 확인 성공", new String[]{String.valueOf(result)});
                        }
                        else {
                            S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 존재 확인 에러 발생", new String[]{ "Error :: channelSftp.ls Is Null" });
                        }

                    }
                    else {
                        S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 존재 확인 에러 발생", new String[]{ "Error :: Input Data Is Null" });
                    }

                }
                else {
                    S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 존재 확인 에러 발생", new String[]{ "Error :: channelSftp.isConnected() == false" });
                }

            }
            else {
                S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 존재 확인 에러 발생", new String[]{ "Error :: mMainCtx Is Null or channelSftp Not Null or isConnection false" });
            }

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

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





    // ------------------------------------------------------------------------------------------
    // TODO [SFTP 서버 파일 삭제 수행]
    // ------------------------------------------------------------------------------------------
    public synchronized boolean deleteFile(String sftpPath, String sftpFileName) {
        S_Log._D_(ACTIVITY_NAME + " :: SFTP 서버 파일 삭제 수행", new String[]{ "sftpPath :: " + String.valueOf(sftpPath), "sftpFileName :: " + String.valueOf(sftpFileName) });

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


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

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

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

                    if (C_Util.stringNotNull(sftpPath) == true && C_Util.stringNotNull(sftpFileName) == true){

                        // [해당 원격 경로 이동 수행]
                        channelSftp.cd(sftpPath);

                        // [저장 된 파일 목록 확인]
                        Vector<ChannelSftp.LsEntry> fileAndFolderList = channelSftp.ls(sftpPath);

                        S_Log._W_(ACTIVITY_NAME + " :: SFTP 서버 파일 리스트 확인", new String[]{String.valueOf(fileAndFolderList)});

                        if (String.valueOf(fileAndFolderList).contains(sftpFileName) == true){

                            // [원격 파일 경로 확인]
                            String sftpPathFile = sftpPath;
                            if (sftpPath.endsWith("/") == true){
                                sftpPathFile += sftpFileName;
                            }
                            else {
                                sftpPathFile += "/" + sftpFileName;
                            }

                            // [SFTP 서버 파일 삭제 수행]
                            channelSftp.rm(sftpPathFile);

                            result = true; // [리턴 변수 삽입]

                            S_Log._W_(ACTIVITY_NAME + " :: SFTP 서버 파일 삭제 수행 성공", new String[]{ "SFTP Path :: " + String.valueOf(sftpPathFile) });

                        }
                        else {
                            S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 삭제 에러 발생", new String[]{ "Error :: Sftp File List Not Found" });
                        }

                    }
                    else {
                        S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 삭제 에러 발생", new String[]{ "Error :: Input Data Is Null" });
                    }

                }
                else {
                    S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 삭제 에러 발생", new String[]{ "Error :: channelSftp.isConnected() == false" });
                }

            }
            else {
                S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 삭제 에러 발생", new String[]{ "Error :: mMainCtx Is Null or channelSftp Not Null or isConnection false" });
            }

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

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





    // ------------------------------------------------------------------------------------------
    // TODO [SFTP 서버 파일 명칭 변경 수행]
    // ------------------------------------------------------------------------------------------
    public synchronized boolean renameFile(String oldSftpPath, String newSftpPath) {
        S_Log._D_(ACTIVITY_NAME + " :: SFTP 서버 파일 삭제 수행", new String[]{ "oldSftpPath :: " + String.valueOf(oldSftpPath), "newSftpPath :: " + String.valueOf(newSftpPath) });


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


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

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

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

                    if (C_Util.stringNotNullMulti(new String[]{oldSftpPath, newSftpPath}) == true){

                        // [SFTP 서버 파일 명칭 변경 수행]
                        channelSftp.rename(oldSftpPath, newSftpPath);

                        result = true; // [리턴 변수 삽입]

                        S_Log._W_(ACTIVITY_NAME + " :: SFTP 서버 파일 명칭 변경 성공", new String[]{ "SFTP Path :: " + String.valueOf(newSftpPath) });

                    }
                    else {
                        S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 명칭 변경 에러 발생", new String[]{ "Error :: Input Data Is Null" });
                    }

                }
                else {
                    S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 명칭 변경 에러 발생", new String[]{ "Error :: channelSftp.isConnected() == false" });
                }

            }
            else {
                S_Log._E_(ACTIVITY_NAME + " :: SFTP 서버 파일 명칭 변경 에러 발생", new String[]{ "Error :: mMainCtx Is Null or channelSftp Not Null or isConnection false" });
            }

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

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


} // TODO [클래스 종료]

 

반응형
Comments