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


接着上一篇說。

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

然后就是往路徑下面傳輸文件了。、

代碼:

 //連接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);
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM