Reference article: http://blog.csdn.net/smallerpig/article/details/50976191
SFTP file Download and ftp file download difference, need to download Jsch-0.1.54jar package
PackageCom.cd.util; Importcom.jcraft.jsch.JSchException; Public classSftpdownloadtestscript { Public Static voidMain (string[] args)throwsjschexception {String ftphost= "192.168.188.155"; String FtpUserName= "Exftp"; String FTPPassword= "sFtP06222013"; intFtpPort = 22; String Ftppath= "."; String LocalPath= "D:/sftp/uploadfiles/demo"; String FileName= "Aa.jpg"; Ftputil.downloadsftpfile (Ftphost, FtpUserName, FTPPassword, FtpPort, Ftppath, LocalPath, fileName); } }
PackageCom.cd.util;ImportJava.io.File; Importjava.io.FileNotFoundException; ImportJava.io.FileOutputStream; Importjava.io.IOException; ImportJava.io.OutputStream; Importjava.net.SocketException; Importjava.util.Properties; ImportOrg.apache.commons.logging.Log; Importorg.apache.commons.logging.LogFactory; ImportCom.jcraft.jsch.Channel; Importcom.jcraft.jsch.ChannelSftp; ImportCom.jcraft.jsch.JSch; Importcom.jcraft.jsch.JSchException; Importcom.jcraft.jsch.Session; Public classFtputil {Private Final StaticLog logger = Logfactory.getlog (ftputil.class); /** Download files from SFTP server * * @param ftphost SFTP IP Address * * @param ftpusername SFTP User name * * @param ftppassword sftp Username Password * * @param ftpport sftp Port * * @param ftppath file path format in SFTP server: ftptes T/AA * * @param localPath download to local location format: h:/download * * @param filename File name*/ Public Static voiddownloadsftpfile (String ftphost, String ftpusername, String FTPPassword,intFtpPort, String Ftppath, String LocalPath, string fileName)throwsJschexception {Session session=NULL; Channel Channel=NULL; Jsch Jsch=NewJsch (); System.out.println (1); Session=jsch.getsession (FtpUserName, Ftphost, FtpPort); System.out.println (2); Session.setpassword (FTPPassword); System.out.println (3); Session.settimeout (100000); System.out.println (4); Properties Config=NewProperties (); System.out.println (5); Config.put ("Stricthostkeychecking", "no"); System.out.println (6); Session.setconfig (config); System.out.println (7); Session.connect (); System.out.println (8); Channel= Session.openchannel ("SFTP"); System.out.println (9); Channel.connect (); Channelsftp chsftp=(CHANNELSFTP) channel; System.out.println (10); String Ftpfilepath= Ftppath + "/" +FileName; String Localfilepath= LocalPath + File.separatorchar +FileName; System.out.println (11); Try{System.out.println ("Ftpfilepath=" +Ftpfilepath); System.out.println ("Localfilepath=" +Localfilepath); System.out.println ("Localpath=" +LocalPath); Chsftp.get (Ftpfilepath, LocalPath); System.out.println (13); } Catch(Exception e) {e.printstacktrace (); Logger.info ("Download error."); } finally{chsftp.quit (); Channel.disconnect (); Session.disconnect (); } } }
The following is the SFTP root directory:
Java code time for the download of sftp files