FTP文件上传下载(C#)

下面是ftp上传下载工具,不能直接运行,请删除不必要的代码。

/// <summary>
    /// ftp文件上传下载
    /// </summary>
    public class FtpHelper
    {
        private string FtpServer;
        private string FtpDefaultUrl;
        private string LogginID;
        private string LoginPWD;
        private string DownloadPath;

        private FtpHelper() { }

        public FtpHelper(string ftpServer, string ftpDefaultUrl, string loginID, string loginPWD, string downloadPath)
        {
            this.FtpServer = ftpServer;
            this.FtpDefaultUrl = ftpDefaultUrl;
            this.LogginID = loginID;
            this.LoginPWD = loginPWD;
            this.DownloadPath = downloadPath;
        }


        public bool DownloadFile(string fileName)
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(string.Format("{0}/{1}", FtpServer+FtpDefaultUrl, fileName));
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.Credentials = new NetworkCredential(LogginID, LoginPWD);

            try
            {
                using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
                {
                    string localfile = Path.Combine(DownloadPath, fileName);
                    FileStream fs = new FileStream(localfile, FileMode.Create, FileAccess.Write);
                    int buffer = 1024;
                    byte[] b = new byte[buffer];
                    int i = 0;
                    Stream stream = ftpResponse.GetResponseStream();
                    while ((i = stream.Read(b, 0, buffer)) > 0)
                    {
                        fs.Write(b, 0, i);
                    }

                    stream.Flush();
                    fs.Flush();
                    stream.Close();
                    fs.Close();
                }
                Log.WriteLog(fileName + "文件下载成功");
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog("ftp下载文件出错:");
                Log.WriteLog(ex);

                return false;
            }
        }


        public bool UploadFile(string localFile, string ftpRemotePath)
        {
            FileInfo file = new FileInfo(localFile);
            FileStream fileStream = file.OpenRead();
            long length = fileStream.Length;
            string url = "";
            if (string.IsNullOrEmpty(ftpRemotePath))
            {
                url = string.Format("{0}/{1}", FtpServer+FtpDefaultUrl, file.Name);
            }
            else
            {
                url = string.Format("{0}/{1}/{2}", FtpServer+FtpDefaultUrl, ftpRemotePath, file.Name);
            }
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(url);
            ftpRequest.Credentials = new NetworkCredential(this.LogginID, this.LoginPWD);
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpRequest.UseBinary = true;
            ftpRequest.ContentLength = length;
            ftpRequest.Timeout = 10 * 1000;

            try
            {
                Stream stream = ftpRequest.GetRequestStream();
                int bufferLength = 2048;
                byte[] b = new byte[bufferLength];

                int i;
                while ((i = fileStream.Read(b, 0, bufferLength)) > 0)
                {
                    stream.Write(b, 0, i);
                }
                stream.Close();
                stream.Dispose();

                return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog("ftp上传文件失败:");
                Log.WriteLog(ex);
                return false;
            }
        }
    }
原文地址:https://www.cnblogs.com/suzixuan/p/6898336.html