C# Sftp操作
阿新 • • 發佈:2017-10-28
cal password protocol none filetype 如果 session delete jsch
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 {View Code3 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 }
測試----使用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
C# Sftp操作