[Java] SFTP 사용하여 파일 업로드

| SFTP를 활용한 파일 업로드


📝 설정

SFTP를 사용하기 위해서는 Jsch dependency를 추가해줘야 한다.

<dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.55</version>
        </dependency>

💻 코드

package e3ps.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Vector;

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

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;

public class SFTPUtil {

    private static final String PMS_IP = "10.1991.28.131";
    private static final String USERNAME = "kk1234";
    private static final String PASSWORD = "kk12#$";
    private static final int PORT = 22;

    private Session session = null;
    private Channel channel = null;
    private ChannelSftp channelSftp = null;

    public static Session getSFTPConnection() throws Exception {

        JSch jsch = new JSch();
        Session session  = null;

        try {
            session  = jsch.getSession(USERNAME, PMS_IP, 22);
            session.setPassword(PASSWORD);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            //session.connect();

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

        return session;
    }

    //디렉토리 생성
    public static void mkdir(ChannelSftp channelSftp, String uploadPath, String dir, String mkdirName) {
        if (!exists(channelSftp, uploadPath)) {
            try {
                channelSftp.cd(dir);
                channelSftp.mkdir(mkdirName);
            } catch (SftpException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //디렉토리 존재
    public static boolean exists(ChannelSftp channelSftp, String uploadPath) {
        Vector res = null;
        try {
            res = channelSftp.ls(uploadPath);
        } catch (SftpException e) {
            if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
                return false;
            }
        }
        return res != null && !res.isEmpty();
    }

    //파일업로드
    public boolean upload(ChannelSftp channelSftp, String dir, File file) {
        boolean isUpload = false;
        SftpATTRS attrs;
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            channelSftp.cd(dir);
            channelSftp.put(in, file.getName());

            // 업로드했는지 확인
            if (exists(channelSftp, dir +"/"+file.getName())) {
                isUpload = true;
            }
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return isUpload;
    }

    //연결종료
    public void disconnection() {
        channelSftp.quit();
        session.disconnect();
    }

    public void disconnection(ChannelSftp channelSftp, Session session) {
        channelSftp.quit();
        session.disconnect();
    }
}

댓글

Designed by JB FACTORY