C# FTP上傳下載 代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;namespace JianKunKing.Common.Ftp { /// <summary> /// ftp方式文件下載上傳 /// </summary> public static class FileUpDownload { #region 變量屬性 /// <summary> /// Ftp服務器ip /// </summary> public static string FtpServerIP = string.Empty; /// <summary> /// Ftp 指定用戶名 /// </summary> public static string FtpUserID = string.Empty; /// <summary> /// Ftp 指定用戶密碼 /// </summary> public static string FtpPassword = string.Empty; #endregion #region 從FTP服務器下載文件,指定本地路徑和本地文件名 /// <summary> /// 從FTP服務器下載文件,指定本地路徑和本地文件名 /// </summary> /// <param name="remoteFileName">遠程文件名</param> /// <param name="localFileName">保存本地的文件名(包含路徑)</param> /// <param name="ifCredential">是否啟用身份驗證(false:表示允許用戶匿名下載)</param> /// <param name="updateProgress">報告進度的處理(第一個參數:總大小,第二個參數:當前進度)</param> /// <returns>是否下載成功</returns> public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, Action<int, int> updateProgress = 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服務器 /// <summary> /// 上傳文件到FTP服務器 /// </summary> /// <param name="localFullPath">本地帶有完整路徑的文件名</param> /// <param name="updateProgress">報告進度的處理(第一個參數:總大小,第二個參數:當前進度)</param> /// <returns>是否下載成功</returns> public static bool FtpUploadFile(string localFullPath, Action<int, int> updateProgress = 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 } } </pre>
調用實例:
下載(不需要帶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); }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!