ftp服務(wù)器搭建部署與C#實現(xiàn)ftp文件的上傳的示例
一、簡介
FTP是File Transfer Protocol(文件傳輸協(xié)議)的英文簡稱,而中文簡稱為“文本協(xié)議”。用于Internet上的控制文件的雙向傳輸。同時,它也是一個應(yīng)用程序(Application)。基于不同的操作系統(tǒng)有不同的FTP應(yīng)用程序,而所有這些應(yīng)用程序都遵守同一種協(xié)議以傳輸文件。在ftp的使用當(dāng)中,用戶經(jīng)常遇到兩個概念:“下載”(Download)和“上傳”(upload)。“下載”文件就是從遠(yuǎn)程主機拷貝文件至自己的計算機上;“上傳”文件就是將文件從自己的計算機中拷貝至遠(yuǎn)程主機上。用Internet語言來說,用戶可通過客戶機程序向(從)遠(yuǎn)程主機上傳(下載)文件。
二、搭建FTP服務(wù)器步驟(Window sserver 2016為例)
安裝FTP服務(wù)器及部署
添加FTP站點
IP地址填本機地址(不填則是本機全部IP),端口默認(rèn)21,SSL是一種數(shù)字加密證書,可申請,在此沒有可選擇無。
添加ftp上傳下載專用用戶(也可以選擇不添加,使用管理員用戶也OK)
到此ftp服務(wù)器安裝和搭建部署,就完成了。
三、登錄測試
瀏覽器中輸入命令 ftp://IP:端口,彈窗提示輸入剛剛新建的用戶名密碼即可。
用戶名和密碼輸入正確的話就會出現(xiàn)公開的路徑。
四、C#上傳文件到FTP服務(wù)器
/// <summary> /// FTP的服務(wù)器地址,格式為ftp://192.168.1.234:8021/。 /// </summary> public string FTPCONSTR { get; set; } /// <summary> /// //FTP服務(wù)器的用戶名 /// </summary> private string FTPUSERID { get; set; } /// <summary> /// //FTP服務(wù)器的密碼 /// </summary> private string FTPPASSWORD { get; set; } private string ftpIP { get; set; } private string ftpPort { get; set; }
public FTPHelper(string ip = "IP", string username = "登錄用戶名", string password = "用戶密碼", string port = "端口") { FTPCONSTR = string.Format("{0}://{1}:{2}/", "ftp", ip, port); FTPUSERID = username; FTPPASSWORD = password; }
/// <summary> /// 上傳文件到遠(yuǎn)程ftp /// </summary> /// <param name="path">本地的文件目錄</param> /// <param name="name">文件名稱</param> /// <returns></returns> public bool UploadFile(string path, string name) { FileInfo f = new FileInfo(path); path = FTPCONSTR + name;//這個路徑是我要傳到ftp目錄下的這個目錄下 FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path)); reqFtp.Method = WebRequestMethods.Ftp.UploadFile; reqFtp.UsePassive = false;//只需要添加這一句話 reqFtp.UseBinary = true; reqFtp.Credentials = new NetworkCredential(FTPUSERID, FTPPASSWORD); reqFtp.KeepAlive = false; reqFtp.Method = WebRequestMethods.Ftp.UploadFile; reqFtp.ContentLength = f.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; FileStream fs = f.OpenRead(); try { Stream strm = reqFtp.GetRequestStream(); contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } strm.Close(); fs.Close(); return true; } catch (Exception ex) { return false; } }
調(diào)用
string txtFilePath=""; try { OpenFileDialog openFileDialogTemp = new OpenFileDialog(); openFileDialogTemp.Title = "選擇要上傳的文件"; DialogResult dr = openFileDialogTemp.ShowDialog(); if (!File.Exists(openFileDialogTemp.FileName)) { GLOBALS.msgbox("內(nèi)容為空,請選擇文件"); return; } if (dr == DialogResult.OK) { txtFilePath= openFileDialogTemp.FileName; string filePath = this.txtFilePath.Text; } } catch (Exception ex) { } string id = DateTime.Now.ToString("yyyyMMddHHmmss"); string isPath = DateTime.Now.ToString("yyyy-MM-dd"); string filePath = txtFilePath; string uploadUrl = isPath + "\\" + id + ".jpg"; FTPHelper FTPHelper = new FTPHelper(); bool uploadresult = FTPHelper.UploadFile(filePath, uploadUrl);
如需ftp檢測目錄是否存在,不存在則創(chuàng)建文件夾,參考以下鏈接
到此這篇關(guān)于ftp服務(wù)器搭建部署與C#實現(xiàn)ftp文件的上傳的示例的文章就介紹到這了,更多相關(guān)C# ftp文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用C#獲取硬盤序列號,CPU序列號,網(wǎng)卡MAC地址的源碼
用C#獲取硬盤序列號,CPU序列號,網(wǎng)卡MAC地址的源碼...2007-03-03C#基于時間輪調(diào)度實現(xiàn)延遲任務(wù)詳解
在很多.net開發(fā)體系中開發(fā)者在面對調(diào)度作業(yè)需求的時候一般會選擇三方開源成熟的作業(yè)調(diào)度框架來滿足業(yè)務(wù)需求,但是有些時候可能我們只是需要一個簡易的延遲任務(wù)。本文主要分享一個簡易的基于時間輪調(diào)度的延遲任務(wù)實現(xiàn),需要的可以參考一下2022-12-12Chrome Visual Studio 2005下的編譯過程
研究Chrome ,首先得把它編譯出來,這對于后續(xù)的代碼分析和閱讀有很大的幫助,想想自己編譯出一個 Chrome 瀏覽器來使用,那是一件很炫的事情。2009-07-07