c#开发-实现从FTP服务器上下载文件夹中的数据
2016-09-12 11:26
489 查看
从FTP服务器上下载文件到本地首先需要确定FTP服务器可以连接,其实现代码如下:
添加的引用为:
代码已经测试通过,可以实现,下载速度大致为12M/s.
/// <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.
相关文章推荐
- C# 下载FTP服务器一个文件夹内的所有文件
- c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
- Java实现ftp上传下载、删除文件及在ftp服务器上传文件夹的方法
- C#学习笔记之从FTP服务器上传和下载数据(二)
- c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
- c#实现 ftp http共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
- C#实现从FTP服务器下载压缩文件并解压.
- C#实现FTP文件夹下载功能【转载】
- C#学习笔记之从FTP服务器上传和下载数据(一)
- C# FTP 文件 文件夹操作 上传 下载
- c#下载服务器文件夹目录下的文件
- 关于FTP client下载进度条的开发与实现思路
- 使用递归方法实现,向FTP服务器上传整个目录结构、从FTP服务器下载整个目录到本地的功能
- C# FTP 文件 文件夹操作 上传 下载
- [C# 网络编程系列]专题十二:实现一个简单的FTP服务器
- Midapex网络开发库v2.2(更新主题:使用C#实现的FTP客户端)
- C#winform 实现从服务器下载
- C#实现FTP上传下载功能
- C#网站开发----在服务器上生成临时文件,下载到客户端,然后删除临时文件
- JAVA实现FTP服务器文件的上传,下载,删除功能