A recently written JAVA Implementation SFTP instance:
/*
* Created on 2009-9-14
* Copyright 2009 by www.xfok.net. All Rights Reserved
*
*/
Package net. xfok. FTP;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. util. properties;
Import java. util. vector;
Import com. jcraft. jsch. channel;
Import com. jcraft. jsch. channelsftp;
Import com. jcraft. jsch. jsch;
Import com. jcraft. jsch. Session;
Import com. jcraft. jsch. sftpexception;
/**
* @ Author yanghua
* Reprinted please indicate the source: http://www.xfok.net/2009/10/124485.html
*/
Public class mysftp {
/**
* Connect to the SFTP Server
* @ Param host
* @ Param Port port
* @ Param Username: Username
* @ Param Password
* @ Return
*/
Public channelsftp connect (string host, int port, string username,
String password ){
Channelsftp SFTP = NULL;
Try {
Jsch = new jsch ();
Jsch. getsession (username, host, Port );
Session sshsession = jsch. getsession (username, host, Port );
System. Out. println ("session created .");
Sshsession. setpassword (password );
Properties sshconfig = new properties ();
Sshconfig. Put ("stricthostkeychecking", "no ");
Sshsession. setconfig (sshconfig );
Sshsession. Connect ();
System. Out. println ("session connected .");
System. Out. println ("Opening channel .");
Channel channel = sshsession. openchannel ("SFTP ");
Channel. Connect ();
SFTP = (channelsftp) channel;
System. Out. println ("connected to" + host + ".");
} Catch (exception e ){
}
Return SFTP;
}
/**
* Upload a file
* @ Param Directory: Directory uploaded
* @ Param uploadfile the file to be uploaded
* @ Param SFTP
*/
Public void upload (string directory, string uploadfile, channelsftp SFTP ){
Try {
SFTP. Cd (directory );
File file = new file (uploadfile );
SFTP. Put (New fileinputstream (file), file. getname ());
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Download an object
* @ Param Directory: Download directory
* @ Param downloadfile the downloaded file
* @ Param SaveFile there is a local path
* @ Param SFTP
*/
Public void download (string directory, string downloadfile, string SaveFile, channelsftp SFTP ){
Try {
SFTP. Cd (directory );
File file = new file (SaveFile );
SFTP. Get (downloadfile, new fileoutputstream (File ));
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Delete an object
* @ Param Directory: directory where the file to be deleted is located
* @ Param deletefile the object to be deleted
* @ Param SFTP
*/
Public void Delete (string directory, string deletefile, channelsftp SFTP ){
Try {
SFTP. Cd (directory );
SFTP. RM (deletefile );
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* List objects in a directory
* @ Param directory the directory to be listed
* @ Param SFTP
* @ Return
* @ Throws sftpexception
*/
Public vector listfiles (string directory, channelsftp SFTP) throws sftpexception {
Return SFTP. ls (directory );
}
Public static void main (string [] ARGs) {
mysftp Sf = new mysftp ();
string host = "192.168.0.1 ";
int Port = 22;
string username = "root";
string Password = "root ";
string directory = "/home/httpd/test/";
string uploadfile = "D: \ TMP \ upload.txt ";
string downloadfile = "upload.txt";
string SaveFile = "D: \ TMP \ download.txt";
string deletefile = "delete.txt ";
channelsftp SFTP = SF. connect (host, port, username, password);
SF. upload (directory, uploadfile, SFTP);
SF. download (directory, downloadfile, SaveFile, SFTP);
SF. delete (directory, deletefile, SFTP);
try {
SFTP. CD (directory);
SFTP. mkdir ("SS");
system. out. println ("finished");
}catch (exception e) {
E. printstacktrace ();
}< BR >}