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

来源:互联网 发布:命令行设置ip地址和mac 编辑:程序博客网 时间:2024/06/07 14:20

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

 /// <summary>        /// 单个文件下载方法        /// </summary>        /// <param name="adss">保存文件的本地路径</param>        /// <param name="ftpadss">下载文件的FTP路径</param>        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();        }        /// </summary>        /// <param name="ftpads">FTP地址路径</param>        /// <param name="name">我们所选择的文件或者文件夹名字</param>        /// <param name="type">要发送到FTP服务器的命令</param>        /// <returns></returns>        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;        }        /// <summary>        /// 下载方法        /// </summary>        /// <param name="ftpads">FTP路径</param>        /// <param name="name">需要下载文件路径</param>        /// <param name="Myads">保存的本地路径</param>        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)                {                    //判断是否具有文件夹标识<DIR>                    if (names.Contains("<DIR>"))                    {                        string olname = names.Split(new string[] { "<DIR>" },                        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.

0 0