c#开发-实现从FTP服务器上下载文件夹中的数据

从FTP服务器上下载文件到本地首先需要确定FTP服务器可以连接,其实现代码如下:

 /// 
        /// 单个文件下载方法
        /// 
        /// 保存文件的本地路径
        /// 下载文件的FTP路径
        public void download(string adss, string ftpadss)
        {
            //FileMode常数确定如何打开或创建文件,指定操作系统应创建新文件。
            //FileMode.Create如果文件已存在,它将被改写
            FileStream outputStream = new FileStream(adss, FileMode.Create);
            FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpadss));
            //设置要发送到 FTP 服务器的命令
            downRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            FtpWebResponse response = (FtpWebResponse)downRequest.GetResponse();
            Stream ftpStream = response.GetResponseStream();
            long cl = response.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();
            response.Close();
        }

        /// 
        /// FTP地址路径
        /// 我们所选择的文件或者文件夹名字
        /// 要发送到FTP服务器的命令
        /// 
        public string[] ftp(string ftpads, string name, string type)
        {
            WebResponse webresp = null;
            StreamReader ftpFileListReader = null;
            FtpWebRequest ftpRequest = null;
            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpads + "/" + name));
                ftpRequest.Method = type;
                webresp = ftpRequest.GetResponse();
                ftpFileListReader = new StreamReader(webresp.GetResponseStream(), Encoding.Default);
            }
            catch (Exception ex)
            {
                ex.ToString();

            }
            StringBuilder str = new StringBuilder();
            string line = ftpFileListReader.ReadLine();
            while (line != null)
            {
                str.Append(line);
                str.Append("/n");
                line = ftpFileListReader.ReadLine();
            }
            string[] fen = str.ToString().Split(new char[2] { '/', 'n' });
            return fen;
        }

        /// 
        /// 下载方法
        /// 
        /// FTP路径
        /// 需要下载文件路径
        /// 保存的本地路径
        public void downftp(string ftpads, string name, string Myads)
        {
            string downloadDir = Myads + "\\" + name;
            string ftpdir = ftpads + "\\" + name;
            string[] fullname = ftp(ftpads, name, WebRequestMethods.Ftp.ListDirectoryDetails);
            //判断是否为单个文件 
            if (fullname.Length <= 2)
            {
                if (fullname[fullname.Length - 1] == "")
                {
                    download(downloadDir + "/" + name, ftpads + "/" + name + "/" + name);
                }
            }
            else
            {
                string[] onlyname = ftp(ftpads, name, WebRequestMethods.Ftp.ListDirectory);
                if (!Directory.Exists(downloadDir))
                {
                    Directory.CreateDirectory(downloadDir);
                }
                foreach (string names in fullname)
                {
                    //判断是否具有文件夹标识
                    if (names.Contains(""))
                    {
                        string olname = names.Split(new string[] { "" },
                        StringSplitOptions.None)[1].Trim();
                        downftp(ftpdir, "//" + olname, downloadDir);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(names))
                        {
                            foreach (string onlynames in onlyname)
                            {
                                if (!string.IsNullOrEmpty(onlynames))
                                {
                                    if (names.Contains(" " + onlynames))
                                    {
                                        download(downloadDir + "/" + onlynames, ftpads + "/" + name + "/" +
                                        onlynames);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }

添加的引用为:

using System;
using System.Text;
using System.IO;
using System.Net;

代码已经测试通过,可以实现,下载速度大致为12M/s.

你可能感兴趣的:(学习笔记)