SFTP is a file protocol that encrypts FTP.
The first is to introduce: Jsch-0.1.42-sources.jar and Jsch-0.1.42.jar Package
sftp file upload and download.
ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.util.Properties;ImportJava.util.Vector;ImportCom.jcraft.jsch.Channel;Importcom.jcraft.jsch.ChannelSftp;ImportCom.jcraft.jsch.JSch;Importcom.jcraft.jsch.Session;Importcom.jcraft.jsch.SftpException;/*** sftp file upload and download **/ Public classSftputil {/*** Connection to SFTP server * *@paramHost * hosts *@paramPort * Ports *@paramUsername * User name *@paramPassword * Password *@return */ PublicChannelsftp Connect (String host,intport, string Username, string password) {channelsftp sftp=NULL; Try{Jsch Jsch=NewJsch (); Jsch.getsession (username, host, port); Session sshsession=jsch.getsession (username, host, port); System.out.println ("Session created."); Sshsession.setpassword (password); Properties Sshconfig=NewProperties (); 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) {}returnsftp; } /*** Upload file * *@paramDirectory * uploaded directories *@paramuploadfile * files to upload *@paramsftp*/ Public voidUpload (String directory, file file, channelsftp sftp) {Try{sftp.cd (directory); Sftp.put (NewFileInputStream (file), File.getname ()); } Catch(Exception e) {e.printstacktrace (); } } /*** Download File * *@paramDirectory * Download Catalog *@paramdownloadFile * Downloaded files *@paramSaveFile * There is a local path *@paramsftp*/ Public voidDownload (string directory, String downloadFile, String saveFile, Channelsftp sftp) {Try{sftp.cd (directory); File File=NewFile (SaveFile); Sftp.get (DownloadFile,Newfileoutputstream (file)); } Catch(Exception e) {e.printstacktrace (); } } /*** Delete Files * *@paramDirectory * To delete directories in which files are located *@paramDeleteFile * files to delete *@paramsftp*/ Public voidDelete (string directory, String deletefile, Channelsftp sftp) {Try{sftp.cd (directory); SFTP.RM (DeleteFile); } Catch(Exception e) {e.printstacktrace (); } } /*** List Files in directory * *@paramDirectory * directories to list *@paramSFTP *@return * @throwssftpexception*/ PublicVector listfiles (String directory, channelsftp sftp)throwssftpexception {returnsftp.ls (directory); } Public Static voidMain (string[] args) {File file=NewFile ("E:/apache-tomcat-7.0.54/running.txt"); Sftputil SF=NewSftputil (); String Host= "114.215.119.231"; intPort = 22; String username= "Root"; String Password= ""; String Directory= "/JAVAWORK/ZMCC"; String UploadFile= "E:/apache-tomcat-7.0.54/running.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 (); } }}
Link Download: Http://pan.baidu.com/s/1mg8bDGW
sftp file Upload and download