用C#连接SFTP服务器并进行上传下载文件

1、使用软件连接可采用WinSCP进行:

文件协议选择SFTP,端口号默认22

2、使用C#代码操作

参考:http://www.cnblogs.com/binw/p/4065642.html

主要引用第三方库Tamir.SharpSSH.dll,同时还要引用DiffieHellman.dll、Org.Mentalis.Security.dll

clsSFTPHelper.cs

  1 using System;
  2 using Tamir.SharpSsh.jsch;
  3 using System.Collections;
  4 
  5 public class clsSFTPHelper
  6 {
  7     private Session m_session;
  8     private Channel m_channel;
  9     private ChannelSftp m_sftp;
 10 
 11     //host:sftp地址   user:用户名   pwd:密码       
 12     public clsSFTPHelper(string server, string user, string pwd, int port)
 13     {
 14         JSch jsch = new JSch();
 15         m_session = jsch.getSession(user, server, port);
 16         MyUserInfo ui = new MyUserInfo();
 17         ui.setPassword(pwd);
 18         m_session.setUserInfo(ui);
 19     }
 20 
 21     //SFTP连接状态       
 22     public bool Connected { get { return m_session.isConnected(); } }
 23 
 24     //连接SFTP       
 25     public bool Connect()
 26     {
 27         try
 28         {
 29             if (!Connected)
 30             {
 31                 m_session.connect();
 32                 m_channel = m_session.openChannel("sftp");
 33                 m_channel.connect();
 34                 m_sftp = (ChannelSftp)m_channel;
 35             }
 36             return true;
 37         }
 38         catch (Exception ex)
 39         {
 40             throw ex;
 41         }
 42     }
 43 
 44     //断开SFTP       
 45     public void Disconnect()
 46     {
 47         if (Connected)
 48         {
 49             m_channel.disconnect();
 50             m_session.disconnect();
 51         }
 52     }
 53 
 54     //SFTP存放文件       
 55     public bool Put(string localPath, string remotePath)
 56     {
 57         try
 58         {
 59             Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(localPath);
 60             Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(remotePath);
 61             m_sftp.put(src, dst);
 62             return true;
 63         }
 64         catch (Exception ex)
 65         {
 66             throw ex;
 67         }
 68     }
 69 
 70     //SFTP获取文件       
 71     public bool Get(string remotePath, string localPath)
 72     {
 73         try
 74         {
 75             Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(remotePath);
 76             Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(localPath);
 77             m_sftp.get(src, dst);
 78             return true;
 79         }
 80         catch
 81         {
 82             return false;
 83         }
 84     }
 85     //删除SFTP文件
 86     public bool Delete(string remoteFile)
 87     {
 88         try
 89         {
 90             m_sftp.rm(remoteFile);
 91             return true;
 92         }
 93         catch
 94         {
 95             return false;
 96         }
 97     }
 98 
 99     //获取SFTP文件列表       
100     public ArrayList GetFileList(string remotePath, string fileType)
101     {
102         try
103         {
104             Tamir.SharpSsh.java.util.Vector vvv = m_sftp.ls(remotePath);
105             ArrayList objList = new ArrayList();
106             foreach (Tamir.SharpSsh.jsch.ChannelSftp.LsEntry qqq in vvv)
107             {
108                 string sss = qqq.getFilename();
109                 if (sss.Length > (fileType.Length + 1) && fileType == sss.Substring(sss.Length - fileType.Length))
110                 { objList.Add(sss); }
111                 else { continue; }
112             }
113 
114             return objList;
115         }
116         catch
117         {
118             return null;
119         }
120     }
121 
122     //登录验证信息       
123     public class MyUserInfo : UserInfo
124     {
125         String passwd;
126         public String getPassword() { return passwd; }
127         public void setPassword(String passwd) { this.passwd = passwd; }
128 
129         public String getPassphrase() { return null; }
130         public bool promptPassphrase(String message) { return true; }
131 
132         public bool promptPassword(String message) { return true; }
133         public bool promptYesNo(String message) { return true; }
134         public void showMessage(String message) { Console.WriteLine(message); }
135 
136     }
137 }
clsSFTPHelper.cs

Program.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace SFTPTest
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             clsSFtpUpload objSFtpUpload = new clsSFtpUpload();
15             objSFtpUpload.SFtpUpload();
16         }
17     }
18 
19     class clsSFtpUpload
20     {
21         public bool SFtpUpload()
22         {
23             bool bolReturn = false;
24             string strFileName = @"D:\test.edi";
25             
26             try
27             {
28                 clsSFTPHelper objSFtp = new clsSFTPHelper("SFtp IP", "UserName", "PAW", 22);
29                 //SFtp IP:服务器的IP,UserName:登录名,PAW:密码
30                 objSFtp.Connect();
31                 if (objSFtp.Connected)
32                 {
33                     Console.Write("Connect Succeed!");
34                     if(objSFtp.Get("/test/test.edi", strFileName))//down Get(RemotePath,LocalPath)
35                     {
36                         Console.Write("Download Succeed!");
37                     }
38                     if (objSFtp.Put(strFileName,"/test/test.edi"))//upload Put(LocalPath,RemotePath)
39                     {
40                         Console.Write("Download Succeed!");
41                     }
42                 }
43                 objSFtp.Disconnect();
44 
45                 bolReturn = true;
46             }
47             catch (Exception ex)
48             {
49                 Console.Write("Exception: " + ex.ToString());
50                 bolReturn = false;
51             }
52             return bolReturn;
53         }        
54     }
55 }
Program.cs

 

posted @ 2017-02-17 16:54  一川  阅读(1650)  评论(0编辑  收藏  举报