c# ftp 上傳文件 與 下載文件

接着上一篇說。服務器

上一篇說了根據配置文件獲取路徑,並判斷路徑在服務器中是否存在。若是不存在則在服務器中創建一個。this

而後就是往路徑下面傳輸文件了。、url

代碼:spa

 //鏈接ftp
        private void Connect(String path)
        {
            // 根據uri建立FtpWebRequest對象
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
            // 指定數據傳輸類型
            reqFTP.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.UsePassive = false;
            // ftp用戶名和密碼
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        }

 

public void Upload(string filename) //上面的代碼實現了從ftp服務器上載文件的功能
        {
            FileInfo fileInf = new FileInfo(filename);
            string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
            Connect(uri);//鏈接          
            // 默認爲true,鏈接不會被關閉
            // 在一個命令以後被執行
            reqFTP.KeepAlive = false;
            // 指定執行什麼命令
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            // 上傳文件時通知服務器文件的大小
            reqFTP.ContentLength = fileInf.Length;
            // 緩衝大小設置爲kb 
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            // 打開一個文件流(System.IO.FileStream) 去讀上傳的文件
            FileStream fs = fileInf.OpenRead();
            try
            {
                int allbye = (int)fileInf.Length;
                int startbye = 0;// 把上傳的文件寫入流
                Stream strm = reqFTP.GetRequestStream();//根據服務器的FTP配置不一樣,要使用不一樣的模式,不然會報錯 // 每次讀文件流的kb 
                contentLen = fs.Read(buff, 0, buffLength);// 流內容沒有結束
                while (contentLen != 0)
                {
                    // 把內容從file stream 寫入upload stream 
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                    startbye += buffLength;
                }// 關閉兩個流
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                this.WriteLog("上傳失敗,緣由: " + ex.Message);

                fs.Close();
            }

        }
/// <summary>
        /// 下載文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Byte[] Download(string fileName)/**/////上面的代碼實現了從ftp服務器下載文件的功能
        {
            Byte[] rtn = null;
            try
            {
                long allbye = (long)GetFileSize(fileName);
                string url = "ftp://" + ftpServerIP + "/" + fileName;
                Connect(url);//鏈接  
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.KeepAlive = false;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount = 0;
                byte[] buffer = new byte[bufferSize];
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                MemoryStream mstream = new MemoryStream();
                int startbye = 0;
                while (readCount > 0)
                {
                    mstream.Write(buffer, 0, readCount);

                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                    startbye += readCount;
                }
                rtn = mstream.ToArray();
                ftpStream.Close();
                response.Close();
                mstream.Close();
                return rtn;
            }
            catch (Exception ex)
            {
                //errorinfo = string.Format("因{0},沒法下載", ex.Message);
                throw new Exception("下載失敗,緣由: " + ex.Message);
            }
        }
相關文章
相關標籤/搜索