728x90
반응형
SFTP 를 사용하기 위한 소스...
라이브러리는 j2ssh-0.2.9-src(http://sourceforge.net/projects/sshtools/(%20j2ssh))를 직접 jar로 만들어 사용...
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 rm(String path) throws Exception
{
boolean rtn = false;
try {
if (sftp != null) {
sftp.rm(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;
}
}
728x90
반응형