A recently written example of a Java implementation of SFTP:
/*
* Created on 2009-9-14
* Copyright 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
* Reprint Please specify source: http://www.xfok.net/2009/10/124485.html
*/
public class Mysftp {
/**
* Connection to SFTP server
* @param host
* @param Port
* @param username User name
* @param password Password
* @return
*/
Public channelsftp Connect (string host, int port, string username,
String password) {
Channelsftp sftp = null;
try {
Jsch 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 Files
* @param directory uploaded by directory
* @param uploadfile files to upload
* @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 File
* @param directory Download Catalog
* @param files downloaded by downloadFile
* The local path exists @param saveFile
* @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 Files
* @param directory where you want to delete files
* @param deletefile The file 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 files in directory
* @param directory to be listed by directory
* @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 ();
}
}
}
Java implementation of an SFTP instance