1、JSch開發包下載
目前最新版本為:
jsch
-0.1.51
2、簡單例子,列出指定目錄下的文件列表
|
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實現SFTP功能一些主要類的介紹
1)JSch 類
2)Session 會話對象類
3)
ChannelSFTP類
ChannelSFTP類是JSch實現SFTP核心類,它提供了一些SFTP常見的操作方法,如下
| 方法名 | 方法說明 |
| put() | 文件上傳 |
| get() | 文件下載 |
|
cd()
|
進入指定目錄 |
|
ls()
|
得到指定目錄下的文件列表 |
|
rename()
|
重命名(移動)指定文件或目錄 |
|
rm()
|
刪除指定文件 |
|
mkdir()
|
創建目錄 |
|
rmdir()
|
刪除目錄(只允許刪除空目錄) |
注:以上這些方法都有很多重載方法
4)
SftpProgressMonitor 傳輸進度監控類
5)
LsEntry 可以認為是文件服務器上的文件條目信息,把包含文件或者目錄相關屬性 。ls命令返回的Vector中的就是LsEntry對象列表
4、SFTP文件傳輸的實現步驟
//1、創建JSch類,好比是FlashFXP工具
JSch jsch = new JSch();
//2、創建本次的文件傳輸會話對象,並連接到SFTP服務器。它好比是通過FlashFXP工具連接到SFTP服務器
session = jsch.getSession(username, host, port);
session.setPassword(passwd);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
try{
//3、在該session會話中開啟一個SFTP通道,之后就可以在該通道中進行文件傳輸了
channelSftp = (ChannelSftp)session.openChannel("sftp");
channelSftp.connect();
}catch(Exception e){
e.printStackTrace();
disConnect();
throw e;
}
//4、進行文件傳輸操作:put()、get()....
channelSftp.put(...)
//5、操作完畢后,關閉通道並退出本次會話
if(channelSftp!=null && channelSftp.isConnected()){
channelSftp.disconnect();
}
if(session!=null && session.isConnected()){
session.disconnect();
}
5、JSch中文亂碼處理
使用
jsch
-0.1.51進行SFTP文件傳輸時,對中文處理會出現亂碼,並且也無法通過setFileNameEncoding()方法來設置編碼。
解決方案:
下載
jsch
-0.1.51源代碼,在
ChannelSFTP.java文件中找到SENDINIT( )方法,修改紅色部分的內容
private void sendINIT() throws Exception {
this.packet.reset();putHEAD((byte)1, 5);this.buf.putInt(3); //修改為 this.buf.putInt(2);getSession().write(this.packet, this, 9);}然后編譯並更改jar中的對應class文件即可。
6、ChannelSftp類的主要API說明如下:
| 方法名 | 方法說明 |
| public void put(String src, String dst) | 將本地文件名為src的文件上傳到目標服務器,目標文件名為dst,若dst為目錄,則目標文件名將與src文件名相同。采用默認的傳輸模式:OVERWRITE |
| public void put(String src, String dst, int mode) | 將本地文件名為src的文件上傳到目標服務器,目標文件名為dst,若dst為目錄,則目標文件名將與src文件名相同。指定文件傳輸模式為mode(mode可選值為:ChannelSftp.OVERWRITE,ChannelSftp.RESUME,ChannelSftp.APPEND)。 |
| public void put(String src, String dst, SftpProgressMonitor monitor) | 將本地文件名為src的文件上傳到目標服務器,目標文件名為dst,若dst為目錄,則目標文件名將與src文件名相同。采用默認的傳輸模式:OVERWRITE,並使用實現了SftpProgressMonitor接口的monitor對象來監控文件傳輸的進度。 |
| public void put(String src, String dst,SftpProgressMonitor monitor, int mode) | 將本地文件名為src的文件上傳到目標服務器,目標文件名為dst,若dst為目錄,則目標文件名將與src文件名相同。指定傳輸模式為mode,並使用實現了SftpProgressMonitor接口的monitor對象來監控文件傳輸的進度。 |
| public void put(InputStream src, String dst) | 將本地的input stream對象src上傳到目標服務器,目標文件名為dst,dst不能為目錄。采用默認的傳輸模式:OVERWRITE |
| public void put(InputStream src, String dst, int mode) | 將本地的input stream對象src上傳到目標服務器,目標文件名為dst,dst不能為目錄。指定文件傳輸模式為mode |
| public void put(InputStream src, String dst, SftpProgressMonitor monitor) | 將本地的input stream對象src上傳到目標服務器,目標文件名為dst,dst不能為目錄。采用默認的傳輸模式:OVERWRITE。並使用實現了SftpProgressMonitor接口的monitor對象來監控傳輸的進度。 |
| public void put(InputStream src, String dst,SftpProgressMonitor monitor, int mode) | 將本地的input stream對象src上傳到目標服務器,目標文件名為dst,dst不能為目錄。指定文件傳輸模式為mode。並使用實現了SftpProgressMonitor接口的monitor對象來監控傳輸的進度。 |
| public OutputStream put(String dst) | 該方法返回一個輸出流,可以向該輸出流中寫入數據,最終將數據傳輸到目標服務器,目標文件名為dst,dst不能為目錄。采用默認的傳輸模式:OVERWRITE |
| public OutputStream put(String dst, final int mode) | 該方法返回一個輸出流,可以向該輸出流中寫入數據,最終將數據傳輸到目標服務器,目標文件名為dst,dst不能為目錄。指定文件傳輸模式為mode |
| public OutputStream put(String dst, final SftpProgressMonitor monitor, final int mode) | 該方法返回一個輸出流,可以向該輸出流中寫入數據,最終將數據傳輸到目標服務器,目標文件名為dst,dst不能為目錄。指定文件傳輸模式為mode。並使用實現了SftpProgressMonitor接口的monitor對象來監控傳輸的進度。 |
| public OutputStream put(String dst, final SftpProgressMonitor monitor, final int mode, long offset) | 該方法返回一個輸出流,可以向該輸出流中寫入數據,最終將數據傳輸到目標服務器,目標文件名為dst,dst不能為目錄。指定文件傳輸模式為mode。並使用實現了SftpProgressMonitor接口的monitor對象來監控傳輸的進度。offset指定了一個偏移量,從輸出流偏移offset開始寫入數據。 |
| get(String src, String dst) | 下載文件到本地,src為目標服務器上的文件,不能為目錄,dst為本地文件路徑。若dst為目錄,則本地文件名與目標服務器上的文件名一樣。 |
| get(String src, String dst ,SftpProgressMonitor monitor) | 同get(String src, String dst),只是該方法允許傳入傳輸進度的監控對象monitor。 |
| get(String src, String dst ,SftpProgressMonitor monitor ,int mode) | 同get(String src, String dst ,SftpProgressMonitor monitor),同時,該方法增加了mode參數,允許指定文件傳輸模式 |
| rm(String path) | 刪除文件,path不能為目錄,刪除目錄使用rmdir |
| rmdir(String path) | 刪除目錄,但是只能刪除空目錄 |
| rename(String oldpath, String newpath) | 如果oldPath為目錄,不要求目錄必須為空 如果newpath為目錄,則newpath必須不能存在,如果已經存在該目錄,則會出現重名或者移動失敗 1、重命名文件或者目錄 2、移動文件或者目錄 |
| ls(String path) | 列出指定目錄下的所有文件和子目錄。該方法返回Vector對象,該列表具體存放的是LsEntry對象 |
