亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ftp服務(wù)器搭建部署與C#實(shí)現(xiàn)ftp文件的上傳的示例

 更新時(shí)間:2022年07月10日 11:38:02   作者:櫻花花  
本文主要介紹了ftp服務(wù)器搭建部署與C#實(shí)現(xiàn)ftp文件的上傳的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、簡(jiǎn)介

FTP是File Transfer Protocol(文件傳輸協(xié)議)的英文簡(jiǎn)稱,而中文簡(jiǎn)稱為“文本協(xié)議”。用于Internet上的控制文件的雙向傳輸。同時(shí),它也是一個(gè)應(yīng)用程序(Application)?;诓煌牟僮飨到y(tǒng)有不同的FTP應(yīng)用程序,而所有這些應(yīng)用程序都遵守同一種協(xié)議以傳輸文件。在ftp的使用當(dāng)中,用戶經(jīng)常遇到兩個(gè)概念:“下載”(Download)和“上傳”(upload)。“下載”文件就是從遠(yuǎn)程主機(jī)拷貝文件至自己的計(jì)算機(jī)上;“上傳”文件就是將文件從自己的計(jì)算機(jī)中拷貝至遠(yuǎn)程主機(jī)上。用Internet語言來說,用戶可通過客戶機(jī)程序向(從)遠(yuǎn)程主機(jī)上傳(下載)文件。

二、搭建FTP服務(wù)器步驟(Window sserver 2016為例)

安裝FTP服務(wù)器及部署   

添加FTP站點(diǎn)

IP地址填本機(jī)地址(不填則是本機(jī)全部IP),端口默認(rèn)21,SSL是一種數(shù)字加密證書,可申請(qǐng),在此沒有可選擇無。

 添加ftp上傳下載專用用戶(也可以選擇不添加,使用管理員用戶也OK) 

 到此ftp服務(wù)器安裝和搭建部署,就完成了。

三、登錄測(cè)試  

瀏覽器中輸入命令 ftp://IP:端口,彈窗提示輸入剛剛新建的用戶名密碼即可。

用戶名和密碼輸入正確的話就會(huì)出現(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;//這個(gè)路徑是我要傳到ftp目錄下的這個(gè)目錄下
            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)容為空,請(qǐng)選擇文件");
                    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檢測(cè)目錄是否存在,不存在則創(chuàng)建文件夾,參考以下鏈接

C# ftp檢測(cè)目錄是否存在和創(chuàng)建文件夾

到此這篇關(guān)于ftp服務(wù)器搭建部署與C#實(shí)現(xiàn)ftp文件的上傳的示例的文章就介紹到這了,更多相關(guān)C# ftp文件上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論