[轉載]SFTP實例

JAVA實現SFTP實例:

/*
 * Created on 2009-9-14
 * Copyright 2009 by www.xfok.net. All Rights Reserved
 *
 */

package net.xfok.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Vector;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;


/**
 * @author YangHua
 * 轉載請注明出處:http://www.xfok.net/2009/10/124485.html
 */
public class MySFTP {
/**
 * 連接sftp服務器
 * @param host 主機
 * @param port 端口
 * @param username 用戶名
 * @param password 密碼
 * @return
 */
    public ChannelSftp connect( String host, int port, String username,
                    String password )
    {
        ChannelSftp sftp = null;
        try {
            JSch jsch = new JSch();
            jsch.getSession( username, host, port );
            Session sshSession = jsch.getSession( username, host, port );
            System.out.println( "Session created." );
            sshSession.setPassword( password );
            Properties sshConfig = new Properties();
            sshConfig.put( "StrictHostKeyChecking", "no" );
            sshSession.setConfig( sshConfig );
            sshSession.connect();
            System.out.println( "Session connected." );
            System.out.println( "Opening Channel." );
            Channel channel = sshSession.openChannel( "sftp" );
            channel.connect();
            sftp = (ChannelSftp) channel;
            System.out.println( "Connected to " + host + "." );
        } catch ( Exception e ) {
        }
        return(sftp);
    }


/**
 * 上傳文件
 * @param directory 上傳的目錄
 * @param uploadFile 要上傳的文件
 * @param sftp
 */
    public void upload( String directory, String uploadFile, ChannelSftp sftp )
    {
        try {
            sftp.cd( directory );
            File file = new File( uploadFile );
            sftp.put( new FileInputStream( file ), file.getName() );
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }


/**
 * 下載文件
 * @param directory 下載目錄
 * @param downloadFile 下載的文件
 * @param saveFile 存在本地的路徑
 * @param sftp
 */
    public void download( String directory, String downloadFile, String saveFile, ChannelSftp sftp )
    {
        try {
            sftp.cd( directory );
            File file = new File( saveFile );
            sftp.get( downloadFile, new FileOutputStream( file ) );
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }


/**
 * 刪除文件
 * @param directory 要刪除文件所在目錄
 * @param deleteFile 要刪除的文件
 * @param sftp
 */
    public void delete( String directory, String deleteFile, ChannelSftp sftp )
    {
        try {
            sftp.cd( directory );
            sftp.rm( deleteFile );
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }


/**
 * 列出目錄下的文件
 * @param directory 要列出的目錄
 * @param sftp
 * @return
 * @throws SftpException
 */
    public Vector listFiles( String directory, ChannelSftp sftp ) throws SftpException
    {
        return(sftp.ls( directory ) );
    }


    public static void main( String[] args )
    {
        MySFTP      sf      = new MySFTP();
        String      host        = "192.168.0.1";
        int     port        = 22;
        String      username    = "root";
        String      password    = "root";
        String      directory   = "/home/httpd/test/";
        String      uploadFile  = "D:\\tmp\\upload.txt";
        String      downloadFile    = "upload.txt";
        String      saveFile    = "D:\\tmp\\download.txt";
        String      deleteFile  = "delete.txt";
        ChannelSftp sftp        = sf.connect( host, port, username, password );
        sf.upload( directory, uploadFile, sftp );
        sf.download( directory, downloadFile, saveFile, sftp );
        sf.delete( directory, deleteFile, sftp );
        try{
            sftp.cd( directory );
            sftp.mkdir( "ss" );
            System.out.println( "finished" );
        }catch ( Exception e ) {
            e.printStackTrace();
        }
    }
}

引用地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,869評論 18 139
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,754評論 18 399
  • 細雨漫過門前路, 往事似水如流年。 輕折傘,卷衣衫,漫步長巷邊, 此景猶從前。 依窗臺,望人生,奈何苦長樂短, 今...
    清風唯賞閱讀 402評論 2 2
  • 今日跑步聽了葉武濱的《時間管理干貨100講》,很好的課程,建議大家在喜馬拉雅搜索該課程去聽課。 R原文:時間管理中...
    貴妃Matilda閱讀 396評論 0 3
  • 在ZMJ集團公司總裝廠只要一提起老孟,人人皆知。 老孟本名孟慶利,別看大家管他叫老孟,其實人并不老——今年剛剛50...
    年輕優秀的飛行員閱讀 1,204評論 73 91