using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; namespace JianKunKing.Common.Ftp { ////// ftp方式文件下載上傳 /// public static class FileUpDownload { #region 變量屬性 ////// Ftp服務器ip /// public static string FtpServerIP = string.Empty; ////// Ftp 指定用戶名 /// public static string FtpUserID = string.Empty; ////// Ftp 指定用戶密碼 /// public static string FtpPassword = string.Empty; #endregion #region 從FTP服務器下載文件,指定本地路徑和本地文件名 ////// 從FTP服務器下載文件,指定本地路徑和本地文件名 /// ///遠程文件名 ///保存本地的文件名(包含路徑) ///是否啟用身份驗證(false:表示允許用戶匿名下載) ///報告進度的處理(第一個參數:總大小,第二個參數:當前進度) ///是否下載成功 public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, ActionupdateProgress = null) { FtpWebRequest reqFTP, ftpsize; Stream ftpStream = null; FtpWebResponse response = null; FileStream outputStream = null; try { outputStream = new FileStream(localFileName, FileMode.Create); if (FtpServerIP == null || FtpServerIP.Trim().Length == 0) { throw new Exception(ftp下載目標服務器地址未設置!); } Uri uri = new Uri(ftp:// + FtpServerIP + / + remoteFileName); ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri); ftpsize.UseBinary = true; reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri); reqFTP.UseBinary = true; if (ifCredential)//使用用戶身份認證 { ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword); reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); } ftpsize.Method = WebRequestMethods.Ftp.GetFileSize; FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse(); long totalBytes = re.ContentLength; re.Close(); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; response = (FtpWebResponse)reqFTP.GetResponse(); ftpStream = response.GetResponseStream(); //更新進度 if (updateProgress != null) { updateProgress((int)totalBytes, 0);//更新進度條 } long totalDownloadedByte = 0; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { totalDownloadedByte = readCount + totalDownloadedByte; outputStream.Write(buffer, 0, readCount); //更新進度 if (updateProgress != null) { updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新進度條 } readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); return true; } catch (Exception) { return false; throw; } finally { if (ftpStream != null) { ftpStream.Close(); } if (outputStream != null) { outputStream.Close(); } if (response != null) { response.Close(); } } } #endregion #region 上傳文件到FTP服務器 /// /// 上傳文件到FTP服務器 /// ///本地帶有完整路徑的文件名 ///報告進度的處理(第一個參數:總大小,第二個參數:當前進度) ///是否下載成功 public static bool FtpUploadFile(string localFullPath, ActionupdateProgress = null) { FtpWebRequest reqFTP; Stream stream = null; FtpWebResponse response = null; FileStream fs = null; try { FileInfo finfo = new FileInfo(localFullPath); if (FtpServerIP == null || FtpServerIP.Trim().Length == 0) { throw new Exception(ftp上傳目標服務器地址未設置!); } Uri uri = new Uri(ftp:// + FtpServerIP + / + finfo.Name); reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri); reqFTP.KeepAlive = false; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用戶,密碼 reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服務器發出下載請求命令 reqFTP.ContentLength = finfo.Length;//為request指定上傳文件的大小 response = reqFTP.GetResponse() as FtpWebResponse; reqFTP.ContentLength = finfo.Length; int buffLength = 1024; byte[] buff = new byte[buffLength]; int contentLen; fs = finfo.OpenRead(); stream = reqFTP.GetRequestStream(); contentLen = fs.Read(buff, 0, buffLength); int allbye = (int)finfo.Length; //更新進度 if (updateProgress != null) { updateProgress((int)allbye, 0);//更新進度條 } int startbye = 0; while (contentLen != 0) { startbye = contentLen + startbye; stream.Write(buff, 0, contentLen); //更新進度 if (updateProgress != null) { updateProgress((int)allbye, (int)startbye);//更新進度條 } contentLen = fs.Read(buff, 0, buffLength); } stream.Close(); fs.Close(); response.Close(); return true; } catch (Exception) { return false; throw; } finally { if (fs != null) { fs.Close(); } if (stream != null) { stream.Close(); } if (response != null) { response.Close(); } } } #endregion } }
調用實例:
下載(不需要帶iis部分的路徑):
FileUpDownload.FtpServerIP = 192.168.1.1; FileUpDownload.FtpUserID = ftpTest001; FileUpDownload.FtpPassword = aaaaaa; FileUpDownload.FtpDownload(Beyond Compare(綠色免安裝).zip, Application.StartupPath + /downloads/crm2.ra6, false);
之前的上傳的文件目錄:
上傳(不需要帶iis部分的路徑):
OpenFileDialog op = new OpenFileDialog(); op.InitialDirectory = Application.StartupPath; op.RestoreDirectory = true; op.Filter = 壓縮文件(*.zip)|*.zip|壓縮文件(*.rar)|*.rar|所有文件(*.*)|*.*; if (op.ShowDialog() == DialogResult.OK) { string aa = op.FileName; FileUpDownload.FtpServerIP = 192.168.1.1; FileUpDownload.FtpUserID = ftpTest001; FileUpDownload.FtpPassword = aaaaaa; //全路徑 FileUpDownload.FtpUploadFile(aa); }