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.

Advertisements

One Comment Add yours

  1. Emanuele says:

    People should read this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s