1, Jsch development package download
http://www.jcraft.com/jsch/
Currently the latest version is: jsch-0.1.51
2, a simple example, listing the list of files in the specified directory
Import java.util.Properties;
Import Java.util.Vector;
Import com.jcraft.jsch.ChannelSftp;
Import Com.jcraft.jsch.JSch;
Import com.jcraft.jsch.Session;
public class Demo003 {
/**
* @param args
*/
public static void Main (string[] args) throws exception{
Jsch Jsch = new Jsch ();
Session session = Jsch.getsession ("CMB", "localhost");
Session.setpassword ("cmb123");
Properties Config = new properties ();
Config.put ("stricthostkeychecking", "no");
Session.setconfig (config);
Session.connect ();
Channelsftp channelsftp = (channelsftp) session.openchannel ("sftp");
Channelsftp.connect ();
Channelsftp.setfilenameencoding ("GBK");
Vector vector = channelsftp.ls ("/");
try {
for (Object obj:vector) {
if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
String FileName = ((com.jcraft.jsch.ChannelSftp.LsEntry) obj). GetFileName ();
System. Out. println (FileName);
}
}
}
finally {
Channelsftp.quit ();
Session.disconnect ();
}
}
}
3, Jsch implementation of the SFTP function of some major classes of introduction
1) Jsch class
2) Session Conversation object class
3) Channelsftp class
The Channelsftp class is the Jsch implementation of the SFTP core class, which provides some common methods of operation for SFTP, as follows
Method name |
Method description |
Put () |
File Upload |
Get () |
File download |
CD () |
Enter the specified directory |
LS () |
Get a list of files in the specified directory |
Rename () |
Rename (move) The specified file or directory |
RM () |
Delete the specified file |
mkdir () |
Create a Directory |
RmDir () |
Delete directory (only empty directories are allowed to be deleted) |
Note: These methods all have many overloaded methods
4) Sftpprogressmonitor Transmission Progress Monitoring class
5) Lsentry can be considered as file entry information on the file server, which contains the file or directory related attributes. The vector returned by the LS command is the list of Lsentry objects
4, the implementation of SFTP file transfer steps
1, create the Jsch class, like the FlashFXP tool
Jsch Jsch = new Jsch ();
2. Create the file transfer Session object for this time and connect to the SFTP server. It is like connecting to an SFTP server via the FLASHFXP tool
Session = Jsch.getsession (username, host, port);
Session. SetPassword (passwd);
Properties Config = new properties ();
Config.put ("stricthostkeychecking", "no");
session. setconfig (config);
session. Connect ();
try {
3. Open an SFTP channel in the session, and then transfer the file to the channel.
Channelsftp = (channelsftp) session. Openchannel ("sftp");
Channelsftp. connect ();
} catch (Exception e) {
E.printstacktrace ();
DisConnect ();
Throw e;
}
4. File transfer operation: put (), get () ....
Channelsftp. Put (...)
5. After the operation is complete, close the channel and exit the session.
if (channelsftp!= null && channelsftp.isconnected ()) {
Channelsftp.disconnect ();
}
if (session!= null && session.isconnected ()) {
Session.disconnect ();
}
5, Jsch Chinese garbled processing
When using jsch-0.1.51 for SFTP file transfer, the Chinese processing is garbled and the encoding cannot be set through the Setfilenameencoding () method.
Solution:
Download the jsch-0.1.51 source code, find the Sendinit () method in the Channelsftp.java file, modify the contents of the Red Section
private void Sendinit () throws Exception {
This.packet.reset ();
Puthead ((Byte) 1, 5);
this.buf.putInt (3); //modified to This.buf.putInt ( 2 );
GetSession (). Write (This.packet, this, 9);
}
You can then compile and change the corresponding class file in the jar.
6. The main API descriptions of the Channelsftp class are as follows:
Method name |
Method description |
public void put (string src, string dst) |
Upload a file with local file name SRC to the target server with the destination file named DST, and if DST is directory, the target file name will be the same as the SRC file name. Using the default transfer mode: OVERWRITE |
public void put (string src, string dst, int mode) |
Upload a file with local file name SRC to the target server with the destination file named DST, and if DST is directory, the target file name will be the same as the SRC file name. Specifies that the file transfer mode is modes (mode selectable values are: Channelsftp.overwrite, Channelsftp.resume, Channelsftp.append). |
public void put (string src, string dst, Sftpprogressmonitor monitor) |
Upload a file with local file name SRC to the target server with the destination file named DST, and if DST is directory, the target file name will be the same as the SRC file name. Use the default transport mode: OVERWRITE, and monitor the progress of file transfers using the monitor object that implements the Sftpprogressmonitor interface. |
public void put (string src, string dst,sftpprogressmonitor Monitor, int mode) |
Upload a file with local file name SRC to the target server with the destination file named DST, and if DST is directory, the target file name will be the same as the SRC file name. Specifies the transfer mode as mode and monitors the progress of the file transfer using the monitor object that implements the Sftpprogressmonitor interface. |
public void put (InputStream src, String DST) |
The local input stream object SRC is uploaded to the destination server with the destination file named DST and DST cannot be a directory. Using the default transfer mode: OVERWRITE |
public void put (InputStream src, String dst, int mode) |
The local input stream object SRC is uploaded to the destination server with the destination file named DST and DST cannot be a directory. Specifies the file transfer mode |
public void put (InputStream src, String DST, Sftpprogressmonitor Monitor) |
The local input stream object SRC is uploaded to the destination server with the destination file named DST and DST cannot be a directory. Use the default transfer mode: OVERWRITE. Monitor the progress of the transmission using the monitor object that implements the Sftpprogressmonitor interface. |
public void put (InputStream src, String dst,sftpprogressmonitor Monitor, int mode) |
The local input stream object SRC is uploaded to the destination server with the destination file named DST and DST cannot be a directory. Specifies that the file transfer mode is modes. Monitor the progress of the transmission using the monitor object that implements the Sftpprogressmonitor interface. |
Public OutputStream put (String DST) |
The method returns an output stream that can write data to the output stream, eventually data to the destination server, the destination file name is DST, and DST cannot be a directory. Using the default transfer mode: OVERWRITE |
Public OutputStream put (String DST, final int mode) |
The method returns an output stream that can write data to the output stream, eventually data to the destination server, the destination file name is DST, and DST cannot be a directory. Specifies the file transfer mode |
Public OutputStream put (String DST, final sftpprogressmonitor monitor, final int mode) |
The method returns an output stream that can write data to the output stream, eventually data to the destination server, the destination file name is DST, and DST cannot be a directory. Specifies that the file transfer mode is modes. Monitor the progress of the transmission using the monitor object that implements the Sftpprogressmonitor interface. |
Public OutputStream put (String DST, final sftpprogressmonitor monitor, final int mode, long offset) |
The method returns an output stream that can write data to the output stream, eventually data to the destination server, the destination file name is DST, and DST cannot be a directory. Specifies that the file transfer mode is modes. Monitor the progress of the transmission using the monitor object that implements the Sftpprogressmonitor interface. OFFSET specifies an offset from which to write data, starting at offset from the output stream. |
Get (string src, string dst) |
Download files to local, src files on the destination server, cannot be directory, DST is local file path. If DST is a directory, the local file name is the same as the file name on the destination server. |
Get (string src, string dst, Sftpprogressmonitor monitor) |
Same as Get (string src, string dst), only the monitor object that the method allows incoming transmission progress. |
Get (string src, string dst, Sftpprogressmonitor monitor, int mode) |
Same as Get (string src, string dst, Sftpprogressmonitor monitor), and the method adds the mode parameter, allowing the specified file transfer mode |
RM (String path) |
Delete file, path cannot be directory, delete directory using rmdir |
RmDir (String Path) |
Delete directories, but only empty directories can be deleted |
Rename (string OldPath, String NewPath) |
If OldPath is a directory, the directory must 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 |
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 |
Using Jsch for SFTP file transfer