C#通過FTP獲取服務端文件
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
一、簡介 實際需求是在前端修改了配置文件后,由上位機統一分發給所有設備,因為下位機支持FTP協議,因此選用FTP來實現文件傳輸功能。 二、準備工作1、FTP服務搭建FTP服務端選用FileZilla Server,免費開源,簡單好用,可以下載中文版的。下載地址:下載 - FileZilla中文網,也可以直接從網盤拿: https://pan.baidu.com/s/1Rss5J2I-3kqzc1qD0CbOcg?pwd=aufr 提取碼: aufr。 2、安裝配置安裝比較簡單,略過不提,主要是有幾個配置要注意下(附上參考文檔,按照這里面的來基本不會有問題:用FileZilla搭建FTP服務器及相關問題 - Better - SegmentFault 思否): 編輯-設置-被動模式設置: 編輯-設置-SSL/TLS設置: 編輯-用戶-General: 編輯-用戶-Shared folders: 偶發出現550報錯: 解決方法:編輯-設置-其他,勾選允許下載: 設置完成后記得啟動服務。 三、客戶端連接1、參數string host;//服務端url,如ftp://127.0.0.1 string username;//用戶名,如root string password;//密碼,如123 string remoteFilePath;//下載文件路徑,如file.db string savePath;//保存文件路徑,如D:\file.db 2、代碼FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(new Uri(host), remoteFilePath)); request.Method = WebRequestMethods.Ftp.DownloadFile; if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password)) request.Credentials = null; else request.Credentials = new NetworkCredential(username, password); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (FileStream fileStream = new FileStream(savePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) { responseStream.CopyTo(fileStream); } 轉自https://www.cnblogs.com/magicMaQaQ/p/18329670 該文章在 2025/3/20 9:28:26 編輯過 |
關鍵字查詢
相關文章
正在查詢... |