Jsch-java implementation of SFTP (file upload detailed article)

Source: Internet
Author: User
Tags file transfer protocol

Http://www.byywee.com/page/M0/S755/755589.html

This article only describes how to apply the SFTP functionality implemented by Jsch.

SFTP is the abbreviation for secure file Transfer protocol, the Enron document transfer peace talks. The transfer file can be thought of as an encryption method of Enron. SFTP is part of SSH and is an Enron format for transferring files to the office. SFTP is the application of encrypted transmission of authentication information and transmission of data, so the application of SFTP is very Enron. But, because this kind of transmission style application encryption/decryption technique, so the transmission effect is much lower than the popular FTP, if you are higher to collect the Enron request, can apply SFTP replace FTP. (from Baidu's statement)

To apply Jsch, you need to download its jar package, download it from the official website: The Http://www.jcraft.com/jsch/ChannelSftp class is the Jsch implementation of the SFTP core class, which contains all of the sftp methods, such as:

Put (): File Upload

Get (): File download

CD (): Enter the specified contents

LS (): Gets the list of files under the specified catalog

Rename (): Renames the specified file or catalog

RM (): Delete the specified file

mkdir (): Create a Catalog

RmDir (): Delete Catalog

And so on (this omits the method parameters, put and get have multiple overloaded methods, see the source code, not listed here.) ) Jsch supports three file transfer modes:
OVERWRITE Full coverage mode, which is the default file transfer mode for Jsch, that is, if the destination file already exists, the transferred file will completely overwrite the target file and generate a new file.
RESUME

Recovery mode, if the file has been transferred to a project group, because of the collection or any other reason for the transmission of the file is interrupted, if the next transmission of the duplicate file,


Will be resumed from the last interrupted premises.

APPEND Append mode, if the destination file already exists, the transferred file will be appended after the target file.

Create a Channelsftp object

Write an object class, according to IP, user name and code to obtain an SFTP channel object, that is, channelsftp instance object, in the application can apply the object to invoke the various types of SFTP operation.

Sftpchannel.java
1  Packagecom.longyg.sftp;2 3 4 5 ImportJava.util.Map;6 7 Importjava.util.Properties;8 9 Ten  One ImportOrg.apache.log4j.Logger; A  -  -  the ImportCom.jcraft.jsch.Channel; -  - Importcom.jcraft.jsch.ChannelSftp; -  + ImportCom.jcraft.jsch.JSch; -  + Importcom.jcraft.jsch.JSchException; A  at Importcom.jcraft.jsch.Session; -  -  -  -  Public classSftpchannel { -  inSession session =NULL; -  toChannel Channel =NULL; +  -  the  *     Private Static FinalLogger LOG = Logger.getlogger (Sftpchannel.class. GetName ()); $ Panax Notoginseng  -  the      PublicChannelsftp Getchannel (map<string, string> sftpdetails,intTimeoutthrowsjschexception { +  A  the  +String Ftphost =Sftpdetails.get (sftpconstants.sftp_req_host); -  $String Port =Sftpdetails.get (sftpconstants.sftp_req_port); $  -String FtpUserName =Sftpdetails.get (sftpconstants.sftp_req_username); -  theString FTPPassword =Sftpdetails.get (Sftpconstants.sftp_req_password); - Wuyi  the  -         intFtpPort =Sftpconstants.sftp_default_port; Wu  -         if(Port! =NULL&&!port.equals ("")) { About  $FtpPort =integer.valueof (port); -  -         } -  A  +  theJsch Jsch =NewJsch ();//Create a Jsch object -  $Session = Jsch.getsession (FtpUserName, Ftphost, FtpPort);//get a Session object by user name, host IP, Port the  theLog.debug ("Session created.")); the  the         if(FTPPassword! =NULL) { -  inSession.setpassword (FTPPassword);//set a cipher the  the         } About  theProperties config =NewProperties (); the  theConfig.put ("stricthostkeychecking", "no"); +  -Session.setconfig (config);//setting properties for session Objects the BayiSession.settimeout (timeout);//When setting timeout the  theSession.connect ();//link through the process session -  -Log.debug ("Session connected.")); the  the  the  theLog.debug ("Opening Channel.")); -  theChannel = Session.openchannel ("sftp");//Open the SFTP channel the  theChannel.connect ();//connections to the SFTP channel94  theLog.debug ("Connected successfully to Ftphost =" + Ftphost + ", as FtpUserName =" +FtpUserName the  the+ ", returning:" +channel);98  About         return(CHANNELSFTP) channel; - 101     }102 103 104  the      Public voidClosechannel ()throwsException {106 107         if(Channel! =NULL) {108 109 Channel.disconnect (); the 111         } the 113         if(Session! =NULL) { the  the Session.disconnect (); the 117         }118 119     } - 121}

Jsch-java implementation of SFTP (file upload detailed article)

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.