C# Sftp操作

      SFTP释义-----引自百度百科

       sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的网络的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的其中一部分,是一种传输档案至 Blogger 伺服器的安全方式。其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件信息传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接和答复操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。

      准备

      Sftp的上传下载等操作用到了C#的第三方类库SharpSSH,需要添加压缩包中的3个dll文件 (Tamir.SharpSSH.dll、Org.Mentalis.Security.dll、DiffieHellman.dll)      下载地址

      在这里给大家介绍微软的一款工具ILMerge,它可以用来将多个dll合并或将dll合并进winform生成的exe程序,笔者这里用来合并上面3个dll,以减少项目的引用方便管理

 

      SftpHelper类-----亲测通过       

  
  1 class sftpHelper
  2     {
  3         private Session m_session;
  4         private Channel m_channel;
  5         private ChannelSftp m_sftp;
  6 
  7         /// <summary>
  8         /// 构造函数
  9         /// </summary>
 10         /// <param name="ip">sftp地址</param>
 11         /// <param name="user">sftp用户名</param>
 12         /// <param name="pwd">sftp密码</param>
 13         /// <param name="port">端口,默认20</param>
 14         public sftpHelper(string ip, string user, string pwd, string port = "20")
 15         {
 16             int serverport = Int32.Parse(port);
 17 
 18             JSch jsch = new JSch();
 19             m_session = jsch.getSession(user, ip, serverport);
 20 
 21             MyUserInfo ui = new MyUserInfo();
 22             ui.setPassword(pwd);
 23             m_session.setUserInfo(ui);
 24         }
 25 
 26         /// <summary>
 27         /// 连接状态
 28         /// </summary>     
 29         public bool Connected { get { return m_session.isConnected(); } }
 30 
 31         /// <summary>
 32         /// 连接SFTP
 33         /// </summary>     
 34         public bool Connect()
 35         {
 36             try
 37             {
 38                 if (!Connected)
 39                 {
 40                     m_session.connect();
 41                     m_channel = m_session.openChannel("sftp");
 42                     m_channel.connect();
 43                     m_sftp = (ChannelSftp)m_channel;
 44                 }
 45                 return true;
 46             }
 47             catch (Exception ex)
 48             {
 49                 return false;
 50             }
 51         }
 52 
 53         /// <summary>
 54         /// 断开SFTP 
 55         /// </summary> 
 56         public void Disconnect()
 57         {
 58             if (Connected)
 59             {
 60                 m_channel.disconnect();
 61                 m_session.disconnect();
 62             }
 63         }
 64 
 65         /// <summary>
 66         /// SFTP存放文件  
 67         /// </summary> 
 68         /// <param name="localPath">本地文件路径</param>
 69         /// <param name="remotePath">sftp远程地址</param>
 70         public bool Put(string localPath, string remotePath)
 71         {
 72             try
 73             {
 74                 if (this.Connected)
 75                 {
 76                     Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(localPath);
 77                     Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(remotePath);
 78                     m_sftp.put(src, dst);
 79                     return true;
 80                 }
 81             }
 82             catch (Exception ex)
 83             {
 84                 return false;
 85             }
 86             return false;
 87         }
 88 
 89         /// <summary>
 90         /// SFTP获取文件  
 91         /// </summary> 
 92         /// <param name="remotePath">sftp远程文件地址</param>
 93         /// <param name="localPath">本地文件存放路径</param>  
 94         public bool Get(string remotePath, string localPath)
 95         {
 96             try
 97             {
 98                 if (this.Connected)
 99                 {
100                     Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(remotePath);
101                     Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(localPath);
102                     m_sftp.get(src, dst);
103                     return true;
104                 }
105             }
106             catch (Exception ex)
107             {
108                 return false;
109             }
110             return false;
111         }
112 
113         /// <summary>
114         /// 删除SFTP文件  
115         /// </summary> 
116         /// <param name="remoteFile">sftp远程文件地址</param>
117         public bool Delete(string remoteFile)
118         {
119             try
120             {
121                 if (this.Connected)
122                 {
123                     m_sftp.rm(remoteFile);
124                     return true;
125                 }
126             }
127             catch
128             {
129                 return false;
130             }
131             return false;
132         }
133 
134 
135         /// <summary>
136         /// 移动SFTP文件  
137         /// </summary> 
138         /// <param name="currentFilename">sftp远程文件地址</param>
139         /// <param name="newDirectory">sftp移动至文件地址</param>
140         public bool Move(string currentFilename, string newDirectory)
141         {
142             try
143             {
144                 if (this.Connected)
145                 {
146                     Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(currentFilename);
147                     Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(newDirectory);
148                     m_sftp.rename(src, dst);
149                     return true;
150                 }
151             }
152             catch (Exception ex)
153             {
154                 return false;
155             }
156             return false;
157         }
158 
159         /// <summary>
160         /// 获取SFTP文件列表  
161         /// </summary> 
162         /// <param name="remotePath">sftp远程文件目录</param>
163         /// <param name="fileType">文件类型</param>
164         public ArrayList GetFileList(string remotePath, string fileType)
165         {
166             try
167             {
168 
169                 if (this.Connected)
170                 {
171                     Tamir.SharpSsh.java.util.Vector vvv = m_sftp.ls(remotePath);
172                     ArrayList objList = new ArrayList();
173                     foreach (Tamir.SharpSsh.jsch.ChannelSftp.LsEntry qqq in vvv)
174                     {
175                         string sss = qqq.getFilename();
176                         if (sss.Length > (fileType.Length + 1) && fileType == sss.Substring(sss.Length - fileType.Length))
177                         { objList.Add(sss); }
178                         else { continue; }
179                     }
180 
181                     return objList;
182                 }
183             }
184             catch
185             {
186                 return null;
187             }
188             return null;
189         }
190 
191     }
192 
193 
194     //登录验证信息         
195     public class MyUserInfo : UserInfo
196     {
197         String passwd;
198 
199         public String getPassword() { return passwd; }
200         public void setPassword(String passwd) { this.passwd = passwd; }
201 
202         public String getPassphrase() { return null; }
203         public bool promptPassphrase(String message) { return true; }
204 
205         public bool promptPassword(String message) { return true; }
206         public bool promptYesNo(String message) { return true; }
207         public void showMessage(String message) { }
208 
209     }
View Code

       

       测试----使用winform

 

   
 1  /// <summary>
 2         /// 下载文件
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void button3_Click(object sender, EventArgs e)
 7         {
 8             FolderBrowserDialog dialog = new FolderBrowserDialog();
 9             if (dialog.ShowDialog() == DialogResult.OK)
10             {
11                 sftpHelper sftp = new sftpHelper("192.168.2.133", "22", "admin", "Admin123");
12                 sftp.Connect();
13                 sftp.Get("/home/20171013103113.csv", dialog.SelectedPath);
14                 sftp.Disconnect();
15             }
16         }
17 
18         /// <summary>
19         /// 上传测试
20         /// </summary>
21         /// <param name="sender"></param>
22         /// <param name="e"></param>
23         private void button2_Click(object sender, EventArgs e)
24         {
25             OpenFileDialog dialog = new OpenFileDialog();
26             dialog.Filter = "|*.csv";
27             if (dialog.ShowDialog() == DialogResult.OK)
28             {
29                 sftpHelper sftp = new sftpHelper("192.168.2.133", "22", "admin", "Admin123");
30                 sftp.Connect();
31                 sftp.Put(dialog.FileName, "/home/");
32                 sftp.Disconnect();
33             }
34             
35         }
36 
37         /// <summary>
38         /// 删除测试
39         /// </summary>
40         /// <param name="sender"></param>
41         /// <param name="e"></param>
42         private void button4_Click(object sender, EventArgs e)
43         {
44             sftpHelper sftp = new sftpHelper("192.168.2.133", "22", "admin", "Admin123");
45             sftp.Connect();
46             sftp.Delete("/home/20171013103113.csv");
47             sftp.Disconnect();
48         }
49 
50         /// <summary>
51         /// 移动测试
52         /// </summary>
53         /// <param name="sender"></param>
54         /// <param name="e"></param>
55         private void button5_Click(object sender, EventArgs e)
56         {
57             sftpHelper sftp = new sftpHelper("192.168.2.133", "22", "admin", "Admin123");
58             sftp.Connect();
59             sftp.Move("/home/20171013103113.csv", "/home/temp/20171013103113.csv");
60             sftp.Disconnect();
61         }
View Code

 

      

 

posted @ 2017-10-28 10:13  kevin_峰  阅读(12191)  评论(2编辑  收藏  举报