Blog Movement

This blog has been moved to this address: http://thefundoowriter.wordpress.com/

Please visit: http://thefundoowriter.wordpress.com/

Sorry for your inconvenience...

How to download securely from sftp server…

Posted by Abhinaba On 4:31 AM


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 Comment

  1. Emanuele Said,

    People should read this.

    Posted on November 10, 2008 at 10:31 AM

     

Post a Comment