public class FtpManager
{
/// <summary>
/// 主机名
/// </summary>
string ftpServerIP;
/// <summary>
/// 登录名
/// </summary>
string ftpUserID;
/// <summary>
/// 登录密码
/// </summary>
string ftpPassword; FtpWebRequest ftpRequest; public FtpManager(string ftpServerIP, string ftpUserID, string ftpPassword)
{
this.ftpServerIP = ftpServerIP; this.ftpUserID = ftpUserID; this.ftpPassword = ftpPassword;
} #region 连接ftp
/// <summary>
/// 连接ftp
/// </summary>
/// <param name="path"></param>
public void Connect(String path)
{ // 根据uri创建FtpWebRequest对象
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "//" + path));
//设置文件传输类型
ftpRequest.UseBinary = true;
//设置登录FTP帐号和密码
ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword); }
#endregion #region FTP获取文件列表 /// <summary>
/// FTP获取文件列表
/// </summary>
/// <param name="ftpServerIP"></param>
/// <param name="ftpUserID"></param>
/// <param name="ftpPassword"></param>
/// <returns></returns>
public string[] FTPGetFileList(string filePath)
{
//响应结果
StringBuilder result = new StringBuilder(); //FTP响应
WebResponse ftpResponse = null; //FTP响应流
StreamReader ftpResponsStream = null; try
{
Connect(filePath); ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
//生成FTP响应
ftpResponse = ftpRequest.GetResponse(); //FTP响应流
ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream()); string line = ftpResponsStream.ReadLine(); while (line != null)
{
result.Append(line);
result.Append("\n");
line = ftpResponsStream.ReadLine();
} //去掉结果列表中最后一个换行
result.Remove(result.ToString().LastIndexOf('\n'), ); //返回结果
return result.ToString().Split('\n');
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return (null);
}
finally
{
if (ftpResponsStream != null)
{
ftpResponsStream.Close();
} if (ftpResponse != null)
{
ftpResponse.Close();
}
}
} #endregion #region FTP下载文件 /// <summary>
/// FTP下载文件
/// </summary>
/// <param name="ftpServerIP">FTP服务器IP</param>
/// <param name="ftpUserID">FTP登录帐号</param>
/// <param name="ftpPassword">FTP登录密码</param>
/// <param name="saveFilePath">保存文件路径</param>
/// <param name="saveFileName">保存文件名</param>
/// <param name="downloadFileName">下载文件名</param>
public void FTPDownloadFile(string path, string saveFileName, string downloadFileName)
{
//定义FTP响应对象
FtpWebResponse ftpResponse = null;
//存储流
FileStream saveStream = null;
//FTP数据流
Stream ftpStream = null; try
{
Connect(downloadFileName);
//生成下载文件
saveStream = new FileStream(path + "\\" + saveFileName, FileMode.Create); //设置下载文件方法
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; //生成FTP响应对象
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); //获取FTP响应流对象
ftpStream = ftpResponse.GetResponseStream(); //响应数据长度
long cl = ftpResponse.ContentLength; int bufferSize = ; int readCount; byte[] buffer = new byte[bufferSize]; //接收FTP文件流
readCount = ftpStream.Read(buffer, , bufferSize); while (readCount > )
{
saveStream.Write(buffer, , readCount); readCount = ftpStream.Read(buffer, , bufferSize);
} }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (ftpStream != null)
{
ftpStream.Close();
} if (saveStream != null)
{
saveStream.Close();
} if (ftpResponse != null)
{
ftpResponse.Close();
}
}
} #endregion #region 从ftp服务器上获得文件列表
/// <summary>
/// 从ftp服务器上获得文件列表
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string[] GetFileList(string path)
{
return FTPGetFileList(path);
}
#endregion #region 文件存在检查
/// <summary>
/// 文件存在检查
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public bool fileCheckExist(string fileName)
{
bool success = false;
FtpWebResponse response = null;
StreamReader reader = null;
try
{
Connect(fileName);
response = (FtpWebResponse)ftpRequest.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
if (line != null)
{
success = true;
}
}
catch (Exception)
{
success = false;
}
finally
{
if (reader != null)
{
reader.Close();
} if (response != null)
{
response.Close();
}
}
return success;
}
#endregion #region 获取文件大小
/// <summary>
/// 获取文件大小
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public long GetFileSize(string filepath)
{
long fileSize = ; try
{
Connect(filepath);//连接 ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize; FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse(); fileSize = response.ContentLength; response.Close();
} catch (Exception ex)
{
throw ex;
} return fileSize; }
#endregion
}

调用方法:

FtpManager client = new FtpManager("192.168.1.101", "wangjun01", "qwer1234");
string parentUrl = "B";
client.FTPDownloadFile("D:", fileList[i], parentUrl);
//string[] fileList = client.FTPGetFileList(parentUrl);
//for (int i = 0; i < fileList.Length; i++)
//{
// client.FTPDownloadFile("D:", fileList[i], parentUrl);
//}

FtpManager类的更多相关文章

  1. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  2. C++ 可配置的类工厂

    项目中常用到工厂模式,工厂模式可以把创建对象的具体细节封装到Create函数中,减少重复代码,增强可读和可维护性.传统的工厂实现如下: class Widget { public: virtual i ...

  3. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

  4. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  5. ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core

    背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...

  6. .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类

    .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...

  7. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  8. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  9. C# 多种方式发送邮件(附帮助类)

    因项目业务需要,需要做一个发送邮件功能,查了下资料,整了整,汇总如下,亲测可用- QQ邮箱发送邮件 #region 发送邮箱 try { MailMessage mail = new MailMess ...

随机推荐

  1. MySQL mysqlimport 从txt文件中导入数据到mysql数据库

    mysqlimport: 我说这个我们还是先从世界观方法论的高度来理解一下便有更加准确的把握.数据导入不外呼有两个部分 第一部分:目标对象--我们要把数据导给谁(mysqlimport 的目标对象自然 ...

  2. 向前辈致敬 strspn

    把8位的CHAR型数据分解为:前5位和后3位,这样2^5 = 32个CHAR型数+值就可表示所有的CHAR型数据 这样做的好处:在给出子串后,不用比较256次,最多比较32次即可判断出是否一个数在子串 ...

  3. 12.04 如何更专业的使用Chrome开发者工具

    顾名思义Chrome开发工具就是一个工具,它允许Web开发人员可以通过浏览器应用程序干预和操作Web页面,也可以通过这个工具调试和测试Web页面或Web应用程序.有了这个工具,你可以做很多有趣的事情: ...

  4. o怎么样racle输入dmp数据库文件

    Oracle进出口数据imp/exp等价物oracle数据恢复和备份. exp命令可以从远程数据库传输数据server出到本地的dmp文件,imp命令能够把dmp文件从本地导入到远处的数据库serve ...

  5. 用js捕捉鼠标连续点击三次事件怎么实现啊

    var count = 0, timer; document.onclick = function(){ if(count < 2){ if(timer){ clearTimeout(timer ...

  6. [NOIP2001提高组]CODEVS1014 Car的旅行路线(最短路)

    最短路,这个不难想,但是要为它加边就有点麻烦..还好写完就过了(虽然WA了一次,因为我调试用的输出没删了..),不然实在是觉得挺难调的.. ------------------------------ ...

  7. 全新安装mysql最新版本

    写在前面: 下面写的东西只是最近安装的一个说明,是在系统中没存在mysql的情况下安装的,后期会根据官方文档写一个详细有价值的文档 安装原理:利用mysql官方的mysql_apt-repositor ...

  8. web开发注意的一些事

    js命名不要包含"-",在chrome浏览器是测试发现,如果文件包含"-",即使指定js本地缓存了,还会向服务器发送请求. cookie path 区分大小写

  9. ThinkPHP 3.1.2 模板中的变量

    一.变量输出 (重点) 1.标量输出 2.数组输出 {$name[1]} {$name['k2']} {$name.k1} 3.对象输出 {$name:k} {$name->k} 二.系统变量 ...

  10. HDU 3328 Flipper

    题解:直接建n个栈,模拟过程即可…… #include <cstdio> #include <cstring> #include <stack> using nam ...