Java code time for the download of sftp files

Source: Internet
Author: User
Tags ftp file

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.