Jsch jar:
http://sourceforge.net/projects/jsch/files/jsch.jar/0.1.53/jsch-0.1.53.jar/download
Jsch API:
http://epaul.github.io/jsch-documentation/javadoc/
範例:
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; public class SFTPUtil { private String host; private String port; private String user; private String pwd; private int timeOut; private Session session; private ChannelSftp channelSftp; private boolean initOK=false; public SFTPUtil (String host,String port,String user,String pwd,int timeOut){ this.host=host; this.port=port; this.user=user; this.pwd=pwd; this.timeOut=timeOut; this.initSFTP(); } private void initSFTP(){ boolean isOK=false; try{ if(setSession()){ if(setChannelSftp()){ isOK=true; System.out.println( "initSFTP : "+"初始化成功"); } } }catch(Exception e){ System.out.println( "initSFTP : "+"init 失敗,錯誤訊息:["+e.getMessage()+"]"); }finally{ initOK=isOK; } } private boolean setSession(){ boolean isOK=false; try { JSch jsch = new JSch(); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); session =jsch.getSession(user, host, Integer.parseInt(port)); session.setPassword(pwd); session.setConfig(sshConfig); session.setTimeout(timeOut * 60 * 1000); session.connect(); if(session.isConnected()){ isOK=true; } } catch (Exception e) { System.out.println( "getSFTPChannel : "+"取得Session連線 失敗,錯誤訊息:["+e.getMessage()+"]"); } return isOK; } private boolean setChannelSftp(){ boolean isOK=false; try { channelSftp = (ChannelSftp)session.openChannel("sftp"); channelSftp.connect(); if(channelSftp.isConnected()){ isOK=true; }else{ this.closeSession(); } } catch (JSchException e) { System.out.println( "getSFTPChannel : "+"取得sftp channel連線 失敗,錯誤訊息:["+e.getMessage()+"]"); this.closeSession(); } return isOK; } private void closeSession(){ if(session!=null){ try{ session.disconnect(); session=null; }catch(Exception e){ System.out.println("closeSession : "+"session's conn close fail. error Message:["+ e.getMessage()+"]"); } } } private void closeChannelSftp(){ if(channelSftp!=null){ try{ channelSftp.disconnect(); channelSftp=null; }catch(Exception e){ System.out.println("closeChannelSftp : "+"channelSftp's conn close fail. error Message:["+ e.getMessage()+"]"); } } } public boolean isInitOK() { return initOK; } /** * 切換目錄,等同cmd的cd * @param path * @return */ public boolean cd(String path){ boolean isOK=false; try { if(initOK){ channelSftp.cd(path); isOK=true; System.out.println( "cd : "+"切換目錄至["+path+"]"); }else{ System.out.println( "cd : "+"切換目錄失敗,初始化未成功不能切換目錄"); } } catch (SftpException e) { System.out.println( "cd : "+"切換目錄失敗,目錄位子:["+path+"],錯誤訊息:["+e.getMessage()+"]"); } return isOK; } /** * 下載檔案,等同cmd的get * @param srcPath 檔案來源 * @param sevePath 儲存位子 * @return */ public boolean get(String srcPath,String sevePath){ boolean isOK=false; FileOutputStream fos=null; try { if(initOK){ fos=new FileOutputStream(sevePath); channelSftp.get(srcPath, fos); isOK=true; System.out.println( "get : "+"下載["+srcPath+"]至["+sevePath+"]"); }else{ System.out.println( "get : "+"下載失敗,初始化未成功不能下載檔案"); } } catch (FileNotFoundException e) { System.out.println( "get : "+"下載失敗,儲存目錄不存在,儲存目錄:["+sevePath+"]"); } catch (SftpException e) { System.out.println( "get : "+"下載失敗,錯誤訊息:["+e.getMessage()+"]"); } finally { try { fos.close(); } catch (IOException e) { System.out.println( "get : "+"close FileOutputStream fail,錯誤訊息:["+e.getMessage()+"]"); } } return isOK; } /** * 下載檔案,等同cmd的put * @param srcPath 檔案來源 * @param sevePath 上傳位子 * @return */ public boolean put(String srcPath,String sevePath){ boolean isOK=false; FileInputStream fis=null; try { if(initOK){ fis=new FileInputStream(srcPath); channelSftp.put(fis, sevePath); isOK=true; System.out.println( "put : "+"上傳["+srcPath+"]至["+sevePath+"]"); }else{ System.out.println( "put : "+"上傳失敗,初始化未成功不能上傳檔案"); } } catch (FileNotFoundException e) { System.out.println( "put : "+"上傳失敗,上傳檔案不存在,檔案路徑:["+srcPath+"]"); } catch (SftpException e) { System.out.println( "put : "+"上傳失敗,錯誤訊息:["+e.getMessage()+"]"); } finally { try { fis.close(); } catch (IOException e) { System.out.println( "put : "+"close FileInputStream fail,錯誤訊息:["+e.getMessage()+"]"); } } return isOK; } /** * 檔案重新命名或移動 * @param oldPath * @param newPath * @return */ public boolean rename(String oldPath,String newPath){ boolean isOK=false; try { if(initOK){ channelSftp.rename(oldPath, newPath); isOK=true; System.out.println( "rename : "+"檔案從["+oldPath+"]換至["+newPath+"]"); }else{ System.out.println( "rename : "+"重新命名或移動失敗,初始化未成功不能重新命名或移動檔案"); } } catch (SftpException e) { System.out.println( "rename : "+"重新命名或移動失敗,錯誤訊息:["+e.getMessage()+"]"); } return isOK; } /** * 刪除檔案 * @param path * @return */ public boolean rm(String path){ boolean isOK=false; try { if(initOK){ channelSftp.rm(path); isOK=true; System.out.println( "rm : "+"刪除["+path+"]檔案"); }else{ System.out.println( "rm : "+"刪除檔案失敗,初始化未成功不能刪除檔案"); } } catch (SftpException e) { System.out.println( "rm : "+"刪除檔案失敗,檔案位子:["+path+"],錯誤訊息:["+e.getMessage()+"]"); } return isOK; } /** * 取得檔案清單 * @param path * @return */ public String[] ls(String path){ String[] names=new String[0]; try { if(initOK){ Vector<?> files=channelSftp.ls(path); if(files.size()>0){ names=new String[files.size()]; for(int n=0;n<files.size();n++){ LsEntry file=(LsEntry)files.get(n); names[n]=file.getFilename(); } } }else{ System.out.println("ls : ", "取得檔案清單失敗,初始化未成功不能取得檔案清單"); } } catch (SftpException e) { System.out.println("ls : ", "取得檔案清單失敗,目錄位子:["+path+"],錯誤訊息:["+e.getMessage()+"]"); } return names; } /** * 如果session或channelSftp是有連線,會關閉它們 */ public void close(){ if(channelSftp!=null&&channelSftp.isConnected()){ this.closeChannelSftp(); } if(session!=null&&session.isConnected()){ this.closeSession(); } initOK=false; System.out.println( "close : "+"已關閉現有連線"); } }
沒有留言:
張貼留言