Java class:
The jar to be referenced:
Jsch-0.1.53.jar
Package Com.isoftstone.www.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;public class Sftptool {/** * 1. First step: Connect to the SFTP server, first get session * * @param host * Host * @param port * Port * @param username * User name * @param password * password * @return */public Sessio n getsession (string host, int port, string username,string password) {Session session = Null;try {Jsch Jsch = new Jsch (); s Ession = Jsch.getsession (username, host, port); System.out.println ("Session created."); Session.setpassword (password); Properties Sshconfig = new properties (); Sshconfig.put ("stricthostkeychecking", "no"); Session.setconfig (Sshconfig); Session.connect (); System.out.println ("Session connected.");} catch (Exception e) {e.printstacktrace (); if (session!= null && session.isconnected ()) {Session.disconnect ();}} return session;} /** * 2. Step two: Connect the SFTP server, then get the link * @return */public channelsftp GetConnect (Session session) {Channelsftp sftp = null;try {if (s Ession = = null) {System.out.println ("Can ' t Create connect,because session is null"); return sftp;} Channel channel = Session.openchannel ("sftp"); System.out.println ("Opening Channel."); Channel.connect (); sftp = (channelsftp) channel; System.out.println ("Connected to" + session.gethost () + ":" +session.getport ());} catch (Exception e) {e.printstacktrace (); if (sftp!= null && sftp.isconnected ()) {Sftp.disconnect ();}} return SFTP;} /** * 3.3rd step: Close the Channel link first * @param channel */public void Disconnect (channel channel) {try {if (channel!= null && Channel.isconnected ()) {channel.disconnect (); SYSTEM.OUT.PRINTLN ("Disconnected channel");}} catch (Exception e) {e.printstacktrace ();}} /** * 4. Fourth Step: Closing Session * @param session */public Void Disconnect (session session) {try {if (session!= nulL && session.isconnected ()) {session.disconnect (); SYSTEM.OUT.PRINTLN ("Disconnected session");}} catch (Exception e) {e.printstacktrace ();}} /** * Upload Files * * @param directory * uploaded directories * @param uploadfile * files to upload * @param sftp */public void up Load (string directory, String uploadfile, channelsftp sftp) {try {sftp.cd (directory); File File = new file (uploadfile), Sftp.put (new FileInputStream (file), File.getname ()),//sftp.put ("d:/application/ Eclipse64ee/workspace/sftp/src/com/isoftstone/www/ftp/sftptool.java ","/data/temp/test ");//upload the local directory file directly to the server} catch (Exception e) {e.printstacktrace ();}} /** * Download File * * @param directory * Download directory * @param downloadFile * downloaded file * @param saveFile * Local Path exists * @param sftp */public void Download (string directory, String downloadfile,string saveFile, channelsftp sftp) {TR Y {sftp.cd (directory); File File = new file (saveFile); Sftp.get (DownloadFile, new FileOutputStream (file));} catch (Exception e) {E.printstackTrace ();}} /** * Delete Files * * @param directory * To delete files in directory * @param deletefile * files to delete * @param sftp */public voi d 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 * directories to list * @param sftp * @return * @throws sftpexception */public Vector L Istfiles (String directory, channelsftp sftp) throws Sftpexception {return sftp.ls (directory);} public static void Main (string[] args) {sftptool sf = new Sftptool (); String directory = "/data/temp/test/";//linux target directory string uploadfile = "D:/temp/upload.txt";//local file to upload string DownloadFile = "Download.txt";//linux the file to be downloaded string saveFile = "D:/temp/download.txt";//file to be saved string deletefile = " Upload.txt ";//file to be deleted string host =" 10.15.22.120 ";//ipint port = 22;//portstring Username =" WebLogic ";//userstring Password = "WebLogic";//passwordsession session = Sf.getsession (host, port, username, PasswoRD); Channelsftp Channel = Sf.getconnect (session), Sf.upload (directory, uploadfile, channel);//upload sf.download (directory, DownloadFile, SaveFile, channel);//download Sf.delete (directory, DeleteFile, channel);//delete sf.disconnect (channel); Sf.disconnect (session); try {//sftp.cd (directory);//sftp.mkdir ("SS"); System.out.println ("Finished");} catch (Exception e) {e.printstacktrace ();}}}
Use Jsch.jar to implement SFTP upload download and delete