Search on this blog

Thursday, February 22, 2007

How to download securely from sftp server…


To use java you need some jar files to import.

Go to this url and get the files.

Next step is coding your program.

Go through the code given below,

package test;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Iterator;

import java.util.List;

import com.sshtools.j2ssh.SftpClient;

import com.sshtools.j2ssh.SshClient;

import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;

import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;

import com.sshtools.j2ssh.configuration.ConfigurationLoader;

import com.sshtools.j2ssh.sftp.FileAttributes;

import com.sshtools.j2ssh.sftp.SftpFile;

public class SFTPTest {

/**

* @param args

* @throws IOException

* @throws JSchException

*/

public static void main(String[] args) throws IOException {

ConfigurationLoader.initialize(true);

String hostname = "192.168.192.14";

// Make a client connection

SshClient ssh = new SshClient();

// Connect to the host

ssh.connect(hostname);

// Create a password authentication instance

PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();

pwd.setPassword("abc123");

pwd.setUsername("abhinaba");

int result = ssh.authenticate(pwd);

if (result == AuthenticationProtocolState.COMPLETE) {

System.out.println("connected");

SftpClient sftp = ssh.openSftpClient();

List fileList = sftp.ls(".");

for (Iterator iter = fileList.iterator(); iter.hasNext();) {

SftpFile file = (SftpFile) iter.next();

FileAttributes fileAttributes = file.getAttributes();

String fileName = file.getFilename();

System.out.println(fileName+" is directory: "+fileAttributes.isDirectory());

if(!fileAttributes.isDirectory()){

try{

sftp.get(fileName,"C:\\Temp\\"+fileName);

}

catch(FileNotFoundException fe){

}

}

}

sftp.quit();

}

ssh.disconnect();

}

}

I think you can understand and write the code now.

1 comments:

Anonymous said...

People should read this.

Post a Comment

Featured Video

Featured Article

Poems