Java SSH/SFTP Library를 활용한 FTP 기능 구현
예전에 SFTP기능 구현이 필요해서 관련 라이브러리와 사용예제를 공유합니다. SSH, SFTP 기능이 필요할 때가 가끔 있죠. 샘플 예제를 살펴보시면 그리 어렵지 않게 구현이 가능합니다.
1. 관련 라이브러리 다운로드
전 SSHTools를 가지고 샘플 예제를 구현해 보았습니다.
2. 구현 샘플
1. 관련 라이브러리 다운로드
- JSch - http://www.jcraft.com/jsch/
- SSHTools - http://sourceforge.net/projects/sshtools/( j2ssh)
전 SSHTools를 가지고 샘플 예제를 구현해 보았습니다.
2. 구현 샘플
package com.mimul.sample;3. 실행 결과
import com.sshtools.j2ssh.SftpClient;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
public class J2sshSftpCient
{
private SshClient client = null;
private PasswordAuthenticationClient auth = null;
private SftpClient sftp = null;
public J2sshSftpCient(String server, String user, String pwd) throws Exception
{
try {
if (server == null || user == null || pwd == null) {
System.out.println("Parameter is null!");
}
client = new SshClient();
client.setSocketTimeout(70000);
client.connect(server);
auth = new PasswordAuthenticationClient();
auth.setUsername(user);
auth.setPassword(pwd);
int result = client.authenticate(auth);
if (result != AuthenticationProtocolState.COMPLETE) {
throw new Exception("Login to " + server + ":22" +
user + "/" + pwd + " failed");
}
sftp = client.openSftpClient();
} catch (Exception e) {
System.out.println(e);
throw e;
}
}
public boolean put(String path) throws Exception
{
boolean rtn = false;
try {
if (sftp != null) {
sftp.put(path);
rtn = true;
}
} catch(Exception e) {
System.out.println(e);
}
return rtn;
}
public boolean get(String srcFile, String destFile) throws Exception
{
boolean rtn = false;
try {
if (sftp != null) {
if (destFile == null)
sftp.get(srcFile);
else
sftp.get(srcFile, destFile);
rtn = true;
}
} catch(Exception e) {
System.out.println(e);
}
return rtn;
}
public boolean lcd(String path) throws Exception
{
boolean rtn = false;
try {
if (sftp != null) {
sftp.lcd(path);
rtn = true;
}
} catch(Exception e) {
System.out.println(e);
}
return rtn;
}
public boolean cd(String path) throws Exception
{
boolean rtn = false;
try {
if (sftp != null) {
sftp.cd(path);
rtn = true;
}
} catch(Exception e) {
System.out.println(e);
}
return rtn;
}
public String pwd() throws Exception
{
String rtnStr = null;
try {
if (sftp != null) {
rtnStr = sftp.pwd();
}
} catch(Exception e) {
System.out.println(e);
}
return rtnStr;
}
public boolean chmod(int permissions, String path) throws Exception
{
boolean rtn = false;
try {
if (sftp != null) {
sftp.chmod(permissions, path);
rtn = true;
}
} catch(Exception e) {
System.out.println(e);
}
return rtn;
}
public boolean isClosed() throws Exception
{
boolean rtn = false;
try {
if (sftp != null)
rtn = sftp.isClosed();
} catch(Exception e) {
System.out.println(e);
}
return rtn;
}
public boolean logout() throws Exception
{
boolean rtn = false;
try {
if (sftp != null)
sftp.quit();
if (client != null)
client.disconnect();
rtn = true;
} catch(Exception e) {
System.out.println(e);
}
return rtn;
}
public static void main(String[] args)
{
try {
J2sshSftpCient jsftp = new J2sshSftpCient("remotehost", "userid",
"password");
boolean test = jsftp.cd("/home/k2/data");
System.out.println(jsftp.pwd());
boolean test1 = jsftp.lcd("C:/");
boolean test2 = jsftp.get("README.TXT", "README.TXT");
boolean isClosed = jsftp.isClosed();
boolean test3 = jsftp.logout();
System.out.println(test + " " + test1 + " " + test2 +
" " + isClosed + " " + test3);
} catch (Exception e) {
System.out.println(e);
}
}
}
Do you want to allow this host key? [Yes|No|Always]: yes 처리후 결과 화면성공적으로 파일이 C:/드라이브에 카피되었습니다. ^^
/home/k2/data
true true true true








