使用JSch實現SFTP檔案傳輸

來源:互聯網
上載者:User

標籤:offset   對象   包含   stat   idt   send   解決方案   tin   列表   

1、JSch開發包下載

http://www.jcraft.com/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 對象

使用JSch實現SFTP檔案傳輸

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.