[Java] SFTP 사용하여 파일 업로드
- 📕 Programing/Java
- 2023. 2. 14. 10:42
| 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();
}
}
'📕 Programing > Java' 카테고리의 다른 글
컴파일(Compile) / 런타임(Runtime) / 컴파일의 흐름 (0) | 2023.05.01 |
---|---|
[Java] equals() / hashcode() (1) | 2023.04.30 |
[Java] 객체의 특정 값으로 오름|내림 차순 정렬 (Comparable 사용) (0) | 2022.04.27 |
[Java] HTTP 방식으로 POST 통신 (1) | 2021.11.02 |
[Java] Steam의 filter 예제 (0) | 2021.10.14 |