Use of the Jsch library

Source: Internet
Author: User


1. Steps to establish a connection

1), Jsch Jsch = new Jsch (); Create a Jsch object

2), Session session = Jsch.getsession (userName, ip, port); Get a Session object based on username, host ip, Port
3), Session.setpassword (password); Set Password

4), Properties config = new properties ();

Config.put ("stricthostkeychecking", "no"); "if set to" Yes ", SSH will not automatically add the Computer's key" $HOME/.ssh/known_hosts "file, and once the Computer's key has changed, refused to connect.

Session.setconfig (config); Setting properties for Session objects

5), Session.settimeout (6000); Set timeout time
Session.connect (); Link creation via session

6), Channel channel = Session.openchannel ("sftp"); Open the SFTP Channel

7), Channel.connect (); Establishing the connection to the SFTP channel
SFTP = (channelsftp) channel;

2. Steps to disconnect

1), channelsftp Disconnect Sftp.disconnect ()
2), Disconnect session Connection Sftp.getsession (). disconnection ()

3. Uploading files to server functions

1), public void put (string src, string dst) uploads a file with the local file name Src to the target server, the target file name is dst, and if DST is the directory, the target file name will be the same as the src file Name. Using the default transfer mode: OVERWRITE
Note SRC is the Windows path format for "d:\\iotest\\testsftp.txt" **************
************* DST for server path format is "/home/test01/zz/testsftp.txt" **************

Three modes of difference:
OVERWRITE 0
Full overwrite mode, that is, if the destination file already exists, the transferred file will completely overwrite the target file, resulting in a new file.
RESUME 1
Recovery mode, If the file has been transferred as part of the file transfer interruption due to network or any other reason, if the same file is transferred the next time
Will be resumed from the last interrupted Location.
APPEND 2
Append mode, If the destination file already exists, the transferred file will be appended after the target File.


2), public void put (string src, string dst, int Mode)
Mode is the transfer pattern (mode selectable value: Channelsftp.overwrite,channelsftp.resume,channelsftp.append)

3), public void put (string src, string dst, sftpprogressmonitor Monitor)
Similar to method one is just the monitor object of the Sftpprogressmonitor interface to monitor the progress of the file Transfer.

4), 5), 6) public void put (inputstream src, String dst)
(5) (6) similar to (2) (3)

7), public OutputStream put (String Dst) The method returns an output stream that can write data to the output stream, and eventually transfer it to the destination server, where the destination file name DST,DST cannot be a directory. Using the default transfer mode: OVERWRITE

8), 9) similar to public outputstream put (String dst, final sftpprogressmonitor monitor, final int Mode)
Just the parameter is final

4. about the Sftpprogressmonitor monitoring function
The code for the interface is:
public interface sftpprogressmonitor{
public static final int put=0;
public static final int get=1;
void init (int op, string src, string dest, long max);
Boolean Count (long count);
void End ();
}
We need to write it ourselves. implementation rewrite three methods init count end

Init (): Call the Init method when the file starts to Transfer.

Count (): When a data block is transferred each time, the Count method is called, and the parameter of the Count method is the size of the data block for this transfer.

End (): Call the End method when the transfer is Complete.

5. File Download

1), public void Get (string src, string dst) The file named SRC on the destination server is downloaded locally, and the local file name is Dst. If DST is a directory, the file name downloaded to local is the same as the SRC file name

2), public void Get (string src, string dst, sftpprogressmonitor monitor), the file named SRC on the target server is downloaded to local, and the local file name is Dst. If DST is a directory, the file name downloaded locally will be the same as the src file Name.

3), public void Get (string src, string dst,sftpprogressmonitor monitor, int mode)//similarly

4), public void Get (String src, outputstream dst) The file named SRC on the destination server is downloaded to local, and the downloaded data is written to the output stream object DST (E.G. file output stream).
5) 6) similarly 2) 3)

7), public InputStream Get (String Src) The method returns an input stream that contains the file data named Src on the destination server. Data can be read from the input stream and eventually transferred to a local

8), public InputStream get (String src, sftpprogressmonitor Monitor)

9), public InputStream get (String src, final sftpprogressmonitor monitor, Finallong Skip)

It contains a monitor progressmonitor similar to the upload;

6. server-assisted Methods

1), rm (String Path) Delete file, path cannot be directory, delete directory use Rmdi

2), rmdir (String Path) to delete the directory, but only empty directories can be deleted

3), rename (string oldpath, string Newpath) If OldPath is a directory, does not require that the directory be empty
If the NewPath is a directory, then NewPath must not exist, and if the directory already exists, the duplicate name will occur or the move fails
1. Renaming files or directories
2. Move files or directories
4), ls (String Path) lists all files and subdirectories under the specified directory. This method returns the vector object, which is a list of Lsentry objects
5), CD (String Path) into the specified directory

Use of the Jsch library

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.