C# FTP下载文件夹到本地

来源:互联网 发布:svg js设置 transform 编辑:程序博客网 时间:2024/06/05 23:08


        /// <summary>        /// FTP文件夹下载到本地文件夹        /// </summary>        /// <param name="ftpPath">ftp路径 </param>        /// <param name="FtpfolderName">ftp文件夹名称</param>        /// <param name="localPath">本地存储路径</param>        /// <param name="localfolderName">本地存储文件夹名称</param>        /// <param name="userId">ftp用户名</param>        /// <param name="password">ftp密码</param>        public void DownLoadFTPAllFolder(            string ftpPath,            string FtpfolderName,            string localPath,            string localfolderName,            string userId,            string password)        {            try            {                // 本地文件存储路径                string localDownloadPath = localPath + "/" + localfolderName;                string FtpUri = ftpPath + (FtpfolderName == null ? null : "/") + FtpfolderName;                FtpWebRequest reqFTP;                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpUri));                reqFTP.Credentials = new NetworkCredential(userId, password);                reqFTP.KeepAlive = false;                reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();                long size = response.ContentLength;                Stream datastream = response.GetResponseStream();                StreamReader sr = new StreamReader(datastream);                string line = sr.ReadLine();                StringBuilder str = new StringBuilder();                while (line != null)                {                    str.Append(line);                    str.Append("|");                    line = sr.ReadLine();                }                // 文件夹数据                string[] datas = str.ToString().Split('|');                for (int i = 0; i < datas.Length; i++)                {                    if (datas[i].Contains("<DIR>"))                    {                        int index = datas[i].IndexOf("<DIR>");                        string name = datas[i].Substring(index + 5).Trim();                        // 创建文件夹                        // 文件夹不存在的场合                        if (!Directory.Exists(localDownloadPath + "/" + name))                        {                            // 创建文件夹                            Directory.CreateDirectory(localDownloadPath + "/" + name);                        }                        // 回调                        DownLoadFTPAllFolder(FtpUri, name, localDownloadPath, name, userId, password);                    }                    else                    {                        // 文件为空的场合                        if (!string.IsNullOrEmpty(datas[i].Trim()))                        {                            string fileName = datas[i].Substring(datas[i].LastIndexOf(" ") + 1);                            // ftp文件路径                            string ftpfilePath = FtpUri + "/" + fileName;                            // 本地文件路径                            string localfilePath = localDownloadPath + "/" + fileName;                            // 读取FTP文件流                            //FileMode.Create如果文件已存在,它将被改写                              // 本地文件流                            FileStream outputStream = new FileStream(localfilePath, FileMode.Create);                            FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpfilePath));                            //设置要发送到 FTP 服务器的命令                              downRequest.Credentials = new NetworkCredential(userId, password);                            downRequest.Method = WebRequestMethods.Ftp.DownloadFile;                            FtpWebResponse responseStream = (FtpWebResponse)downRequest.GetResponse();                            // ftp文件流                            Stream ftpStream = responseStream.GetResponseStream();                            long cl = responseStream.ContentLength;                            int bufferSize = 2048;                            int readCount;                            byte[] buffer = new byte[bufferSize];                            readCount = ftpStream.Read(buffer, 0, bufferSize);                            while (readCount > 0)                            {                                outputStream.Write(buffer, 0, readCount);                                readCount = ftpStream.Read(buffer, 0, bufferSize);                            }                            ftpStream.Close();                            outputStream.Close();                            responseStream.Close();                        }                    }                }                sr.Close();                datastream.Close();                response.Close();            }            catch (Exception ex)            {                throw ex;            }        }               /// <summary>        /// 下载FTP文件        /// </summary>        /// <param name="userId">userId</param>        /// <param name="password">password</param>        /// <param name="ftpfilePath">ftpfilePath</param>        /// <param name="localfilePath">localfilePath</param>        private void downLoadftpFile(string userId, string password, string ftpfilePath, string localfilePath)        {            // 读取FTP文件流            //FileMode.Create如果文件已存在,它将被改写              // 本地文件流            FileStream outputStream = new FileStream(localfilePath, FileMode.Create);            FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpfilePath));            //设置要发送到 FTP 服务器的命令              downRequest.Credentials = new NetworkCredential(userId, password);            downRequest.Method = WebRequestMethods.Ftp.DownloadFile;            FtpWebResponse responseStream = (FtpWebResponse)downRequest.GetResponse();            // ftp文件流            Stream ftpStream = responseStream.GetResponseStream();            long cl = responseStream.ContentLength;            int bufferSize = 2048;            int readCount;            byte[] buffer = new byte[bufferSize];            readCount = ftpStream.Read(buffer, 0, bufferSize);            while (readCount > 0)            {                outputStream.Write(buffer, 0, readCount);                readCount = ftpStream.Read(buffer, 0, bufferSize);            }            ftpStream.Close();            outputStream.Close();            responseStream.Close();        }


文章可能还有疏落的地方,请大家有问题指教以下。

原创粉丝点击