SFTP유틸 클래스 추가
parent
90b2c623cf
commit
6077a6b468
Binary file not shown.
@ -0,0 +1,161 @@
|
|||||||
|
package cfs.common.util;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
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.SftpException;
|
||||||
|
|
||||||
|
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressFBWarnings
|
||||||
|
@Component
|
||||||
|
public class SftpUtil {
|
||||||
|
|
||||||
|
static Logger logger = Logger.getLogger(SftpUtil.class.getName());
|
||||||
|
|
||||||
|
private Session session;
|
||||||
|
private Channel channel;
|
||||||
|
private ChannelSftp channelSftp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 서버와 연결에 필요한 값들을 가져와 초기화 시킴
|
||||||
|
* @param host 서버 주소
|
||||||
|
* @param userName 접속에 사용될 아이디
|
||||||
|
* @param passWord 비밀번호
|
||||||
|
* @param port 포트번호
|
||||||
|
*/
|
||||||
|
public int checkSftpServerStatus(String host, String userName, String passWord, int port, String path) {
|
||||||
|
|
||||||
|
int step = 1;
|
||||||
|
|
||||||
|
boolean exception = false;
|
||||||
|
|
||||||
|
JSch jsch = new JSch();
|
||||||
|
|
||||||
|
try {
|
||||||
|
session = jsch.getSession(userName, host, port);
|
||||||
|
session.setPassword(passWord);
|
||||||
|
|
||||||
|
Properties config = new Properties();
|
||||||
|
config.put("StrictHostKeyChecking", "no");
|
||||||
|
session.setConfig(config);
|
||||||
|
session.connect();
|
||||||
|
channel = session.openChannel("sftp");
|
||||||
|
channel.connect();
|
||||||
|
if(!channel.isConnected()){
|
||||||
|
return step;
|
||||||
|
}
|
||||||
|
|
||||||
|
channelSftp = (ChannelSftp) channel;
|
||||||
|
|
||||||
|
step++;
|
||||||
|
|
||||||
|
channelSftp.cd(path);
|
||||||
|
|
||||||
|
channelSftp.disconnect();
|
||||||
|
|
||||||
|
|
||||||
|
} catch (JSchException e) {
|
||||||
|
exception = true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
exception = true;
|
||||||
|
} finally {
|
||||||
|
if (channelSftp != null && channelSftp.isConnected()){
|
||||||
|
try {
|
||||||
|
channelSftp.disconnect();
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
exception = true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
exception = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(exception){
|
||||||
|
return step;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean init(String host, String userName, String passWord, int port) {
|
||||||
|
|
||||||
|
JSch jsch = new JSch();
|
||||||
|
|
||||||
|
try {
|
||||||
|
session = jsch.getSession(userName, host, port);
|
||||||
|
session.setPassword(passWord);
|
||||||
|
|
||||||
|
Properties config = new Properties();
|
||||||
|
config.put("StrictHostKeyChecking", "no");
|
||||||
|
session.setConfig(config);
|
||||||
|
session.connect();
|
||||||
|
channel = session.openChannel("sftp");
|
||||||
|
channel.connect();
|
||||||
|
channelSftp = (ChannelSftp) channel;
|
||||||
|
|
||||||
|
} catch (JSchException e) {
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 하나의 파일을 업로드 한다.
|
||||||
|
* @param dir 저장시킬 주소(서버)
|
||||||
|
* @param file 저장할 파일
|
||||||
|
*/
|
||||||
|
public boolean sFtpUpload(String strDir, File file) {
|
||||||
|
|
||||||
|
boolean isResult = true;
|
||||||
|
FileInputStream in = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
in = new FileInputStream(file);
|
||||||
|
if(channelSftp !=null){
|
||||||
|
channelSftp.cd(strDir);
|
||||||
|
channelSftp.put(in, file.getName());
|
||||||
|
}else{
|
||||||
|
isResult = false;
|
||||||
|
}
|
||||||
|
} catch (SftpException e) {
|
||||||
|
isResult = false;
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
isResult = false;
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
sFtpDisConnection();
|
||||||
|
if(in != null)
|
||||||
|
in.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.info(e);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.info(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sFtpDisConnection() {
|
||||||
|
if(channelSftp != null){
|
||||||
|
channelSftp.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue