sftp

转载 2011年01月09日 18:34:00


package net.xwolf.ultility;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

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; public class FtpImpl{
   
   
private String host = "127.0.0.1";
   
private String username="MingMac";
   
private String password="×××";
   
private int port = 22;
   
private ChannelSftp sftp = null;
   
private String localPath = "/Users/MingMac/Documents";
   
private String remotePath = "/Users/MingMac/MyDocuments";
   
private String fileListPath = "/Users/MingMac/Documents/Java/workspace/MyTools/conf/file.txt";
   
private final String seperator = "/";
   
   
/**
     * connect server via sftp
    
*/
   
public void connect() {
       
try {
           
if(sftp != null){
                System.out.println(
"sftp is not null");
            }
            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) {
            e.printStackTrace();
        }
    }
   
/**
     * Disconnect with server
    
*/
   
public void disconnect() {
       
if(this.sftp != null){
           
if(this.sftp.isConnected()){
               
this.sftp.disconnect();
            }
else if(this.sftp.isClosed()){
                System.out.println(
"sftp is closed already");
            }
        }

    }

   
public void download() {
       
// TODO Auto-generated method stub
       

    }
   
   
   
private 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();
        }
    }
   
   
/**
     * upload all the files to the server
    
*/
   
public void upload() {
        List
<String> fileList = this.getFileEntryList(fileListPath);
       
try {
           
if(fileList != null){
               
for (String filepath : fileList) {
                    String localFile
= this.localPath + this.seperator+ filepath;
                    File file
= new File(localFile);
                   
                   
if(file.isFile()){
                        System.out.println(
"localFile : " + file.getAbsolutePath());
                        String remoteFile
= this.remotePath + this.seperator + filepath;
                        System.out.println(
"remotePath:" + remoteFile);
                        File rfile
= new File(remoteFile);
                        String rpath
= rfile.getParent();
                       
try {
                            createDir(rpath, sftp);
                        }
catch (Exception e) {
                            System.out.println(
"*******create path failed" + rpath);
                        }

                       
this.sftp.put(new FileInputStream(file), file.getName());
                        System.out.println(
"=========upload down for " + localFile);
                    }
                }
            }
        }
catch (FileNotFoundException e) {
           
// TODO Auto-generated catch block
            e.printStackTrace();
        }
catch (SftpException e) {
           
// TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }
   
/**
     * create Directory
     *
@param filepath
     *
@param sftp
    
*/
   
private void createDir(String filepath, ChannelSftp sftp){
       
boolean bcreated = false;
       
boolean bparent = false;
        File file
= new File(filepath);
        String ppath
= file.getParent();
       
try {
           
this.sftp.cd(ppath);
            bparent
= true;
        }
catch (SftpException e1) {
            bparent
= false;
        }
       
try {
           
if(bparent){
               
try {
                   
this.sftp.cd(filepath);
                    bcreated
= true;
                }
catch (Exception e) {
                    bcreated
= false;
                }
               
if(!bcreated){
                   
this.sftp.mkdir(filepath);
                    bcreated
= true;
                }
               
return;
            }
else{
                createDir(ppath,sftp);
               
this.sftp.cd(ppath);
               
this.sftp.mkdir(filepath);
            }
        }
catch (SftpException e) {
            System.out.println(
"mkdir failed :" + filepath);
            e.printStackTrace();
        }
       
       
try {
           
this.sftp.cd(filepath);
        }
catch (SftpException e) {
            e.printStackTrace();
            System.out.println(
"can not cd into :" + filepath);
        }
       
    }
   
/**
     * get all the files need to be upload or download
     *
@param file
     *
@return
    
*/
   
private List<String> getFileEntryList(String file){
        ArrayList
<String> fileList = new ArrayList<String>();
        InputStream in
= null;
       
try {
           
            in
= new FileInputStream(file);
            InputStreamReader inreader
= new InputStreamReader(in);
           
            LineNumberReader linreader
= new LineNumberReader(inreader);
            String filepath
= linreader.readLine();
           
while(filepath != null){
                fileList.add(filepath);
                filepath
= linreader.readLine();
            }
            in.close();
        }
catch (FileNotFoundException e) {
           
// TODO Auto-generated catch block
            e.printStackTrace();
        }
catch (IOException e) {
           
// TODO Auto-generated catch block
            e.printStackTrace();
        }
finally{
           
if(in != null){
                in
= null;
            }
        }

       
return fileList;
    }

   
/**
     *
@return the host
    
*/
   
public String getHost() {
       
return host;
    }

   
/**
     *
@param host the host to set
    
*/
   
public void setHost(String host) {
       
this.host = host;
    }

   
/**
     *
@return the username
    
*/
   
public String getUsername() {
       
return username;
    }

   
/**
     *
@param username the username to set
    
*/
   
public void setUsername(String username) {
       
this.username = username;
    }

   
/**
     *
@return the password
    
*/
   
public String getPassword() {
       
return password;
    }

   
/**
     *
@param password the password to set
    
*/
   
public void setPassword(String password) {
       
this.password = password;
    }

   
/**
     *
@return the port
    
*/
   
public int getPort() {
       
return port;
    }

   
/**
     *
@param port the port to set
    
*/
   
public void setPort(int port) {
       
this.port = port;
    }

   
/**
     *
@return the sftp
    
*/
   
public ChannelSftp getSftp() {
       
return sftp;
    }

   
/**
     *
@param sftp the sftp to set
    
*/
   
public void setSftp(ChannelSftp sftp) {
       
this.sftp = sftp;
    }

   
/**
     *
@return the localPath
    
*/
   
public String getLocalPath() {
       
return localPath;
    }

   
/**
     *
@param localPath the localPath to set
    
*/
   
public void setLocalPath(String localPath) {
       
this.localPath = localPath;
    }

   
/**
     *
@return the remotePath
    
*/
   
public String getRemotePath() {
       
return remotePath;
    }

   
/**
     *
@param remotePath the remotePath to set
    
*/
   
public void setRemotePath(String remotePath) {
       
this.remotePath = remotePath;
    }

   
/**
     *
@return the fileListPath
    
*/
   
public String getFileListPath() {
       
return fileListPath;
    }

   
/**
     *
@param fileListPath the fileListPath to set
    
*/
   
public void setFileListPath(String fileListPath) {
       
this.fileListPath = fileListPath;
    }
   
   
public static void main(String[] args) {
       
// TODO Auto-generated method stub
        FtpImpl ftp= new FtpImpl();
        ftp.connect();
        ftp.upload();
        ftp.disconnect();
        System.exit(
0);
    }


}

SFTP(C#)

1、SFTP服务端搭建 2、SFTP可视化客户端 3、C#封装SFTP帮助类 4、SFTP测试上传文件
  • 2017年09月04日 22:01

sftp资料sftp资料

  • 2011年04月11日 21:52
  • 361KB
  • 下载

sftp源码sftp源码sftp源码

  • 2011年03月07日 01:41
  • 31KB
  • 下载

FTP,SFTP命令

SFTP命令汇总                   cd 路径                        更改到远程目录的路径                   lcd 路径        ...
  • yonghutwo
  • yonghutwo
  • 2014-12-14 18:27:55
  • 2598

FTP/SFTP简单使用

SCP、SFTP命令基本使用
  • lupeng0527
  • lupeng0527
  • 2017-06-10 10:45:50
  • 379

SFTP客户端代码示例

SFTP客户端代码示例 环境:libssh2 1.4.3、zlib-1.2.8、openssl-1.0.1g Author: Kagula 最后更新日期:2014-5-18          ...
  • lee353086
  • lee353086
  • 2014-10-21 20:01:22
  • 10579

sftp基本使用方法

sftp 是一个交互式文件传输程式。它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性。下边就简单介绍一下如何远程连接主机,进行文件的上传和下载,以及一些相关操作。 在命令行模式下,有...
  • liranke
  • liranke
  • 2011-09-16 16:33:39
  • 6554

在Windows Server上搭建SFTP

目前的IIS7仍然不能创建SFTP站点,所以需要借助第3方工具 感觉最方便,最小巧,最易用的就是WinSSHD...
  • liangpei2008
  • liangpei2008
  • 2011-11-22 08:19:16
  • 5619

linux sftp远程连接命令

linux sftp远程连接命令   sftp -oPort=60001 root@192.168.0.254   使用-o选项来指定端口号。   -oPort=远程端口号  ...
  • gaoqiao1988
  • gaoqiao1988
  • 2016-11-02 16:43:43
  • 13372

sftp与ftp用法

sftp与ftp用法
  • keda8997110
  • keda8997110
  • 2016-03-02 15:24:30
  • 3172
收藏助手
不良信息举报
您举报文章:sftp
举报原因:
原因补充:

(最多只允许输入30个字)