首先在Nuget package下载下面的dll,第三方封装好的:

using FluentFTP;
using System;
using System.IO;
using System.Net;namespace Test
{public class FTPHelper{private FtpClient ftp;public bool Connected { get { return ftp.IsConnected; } }public void InitClient(string server, string port, string user, string pwd){try{ftp = new FtpClient(server, Int32.Parse(port), new NetworkCredential(user, pwd));}catch (Exception ex){throw ex;}}public bool Connect(){try{if (!Connected){ftp.Connect();}return true;}catch (Exception ex){throw ex;return false;}}public void Disconnect(){try{if (ftp != null && Connected){ftp.Disconnect();}}catch (Exception ex){throw ex;}}public void ArchiveFile(string oldPath, string newPath, string fileName){try{if (Connect()){if (!ftp.DirectoryExists(newPath))ftp.CreateDirectory(newPath);ftp.Rename(oldPath + "/" + fileName, newPath + "/" +Path.GetFileNameWithoutExtension(fileName) + "_" +DateTime.Now.ToString("yyyyMMddHHmmssffff") + Path.GetExtension(fileName));}}catch (Exception ex){throw ex;}}public void DownloadFiles(string server, string port, string user, string password, string localPath, string remotePath, string archiveFilePath, string fileType, bool recursive, bool archive){try{InitClient(server, port, user, password);if (Connect()){foreach (var ftpListItem in ftp.GetListing(remotePath)){RecursiveFiles(ftpListItem, fileType, localPath, remotePath, archiveFilePath, recursive, archive);}}}catch (Exception ex){throw ex;}finally{Disconnect();}}public void RecursiveFiles(FtpListItem file, string fileType, string localPath,string remotePath, string archiveFilePath, bool recursive, bool archive){try{if (file.Type.ToString().Equals("Directory")){if (recursive){foreach (var recursiveFile in ftp.GetListing(file.FullName)){RecursiveFiles(recursiveFile, fileType, localPath,file.FullName,archiveFilePath, recursive, archive);}}}else{ProcessFiles(file, fileType, localPath, remotePath, archiveFilePath, archive);}}catch (Exception ex){throw ex;}}public void ProcessFiles(FtpListItem file, string fileType, string localPath,string remotePath, string archiveFilePath, bool archive){string fileName = file.Name;if (!Directory.Exists(localPath))Directory.CreateDirectory(localPath);try{if (fileType.Equals("*")){WriteFileToLocalStorage(file, fileName, localPath, remotePath, archiveFilePath, archive);}else{string[] fileTypes = fileType.Split(',');foreach (var type in fileTypes){if (type.ToLower().Equals(Path.GetExtension(fileName).ToLower())){WriteFileToLocalStorage(file, fileName, localPath, remotePath, archiveFilePath, archive);}}}}catch (Exception ex){throw ex;}}private void WriteFileToLocalStorage(FtpListItem file, string fileName, string localPath, string remotePath, string archiveFilePath, bool archive){Stream ftpStream = null;FileStream fileStream = null;try{string destinationPath = string.Format(@"{0}\{1}", localPath, file.Name);ftpStream = ftp.OpenRead(file.FullName);if(ftpStream.Length > 0){fileStream = File.Create(destinationPath, (int)ftpStream.Length);byte[] buffer = new byte[200 * 1024];int count;while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0){fileStream.Write(buffer, 0, count);}}if (archive && !string.IsNullOrEmpty(archiveFilePath)){ArchiveFile(remotePath, archiveFilePath, fileName);}}catch (Exception ex){throw ex;}finally{if(ftpStream != null){ftpStream.Flush();ftpStream.Close();}if (fileStream != null){fileStream.Flush();fileStream.Close();}}}}
}

c# 使用FluentFTP进行ftp下载文件相关推荐

  1. C#FTP下载文件出现远程服务器返回错误: (500) 语法错误,无法识别命令

    如果下载多个文件的时候,有时候莫名其妙的出现500服务器错误,很有可能是没有设置KeepAlive 属性导致的. 出现应用程序未处理的异常:2015/1/6 11:40:56 异常类型:WebExce ...

  2. 使用FTP下载文件connect.retrieveFileStream(filename) 获取不到InputStream流,返回null的问题

    使用同事的代码做FTP下载文件,InputStream in = connect.retrieveFileStream(fileName);执行这句时InputStream总是获取为空 后来把代码改成 ...

  3. Java实现FTP下载文件到客户端(浏览器)

    目录 一.简介 二.maven依赖 三.配置类 四.工具类 4.1.服务器文件名中文处理 4.2.下载文件名中文处理 五.接口验证 一.简介   我在之前的文章(Java实现文件上传和下载)里讲过非F ...

  4. 【踩坑】Linux java中ftp下载文件,解压文件损坏,以及图片下载打开只显示下载路径的问题

    [踩坑]Linux java中ftp下载文件,解压文件损坏,以及图片下载打开只显示下载路径的问题 一. 问题重现 二. 问题解决思路 1. 确认是不是上传就导致数据出错了 2. 是不是平台问题 三. ...

  5. 从ftp下载文件(word)到本地显示文件损坏或错误

    今天遇到问题从ftp下载文件到本地路径无法查看,发现是文件名称中含有比如空格,问号等非常规字符存在,虽然在本地创建了对应的文件,但是流无法识别一些非常规字符,所以即使下载到本地也显示文件损坏或错误.我 ...

  6. 使用FTP下载文件资源

    使用FTP下载文件(新手向) 1.FTP介绍 FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文件传输协议".用于Internet上的 ...

  7. linux中如何用ftp命令下载文件,linux中ftp下载文件命令的用法

    linxu下的ftp命令是用来下载文件或者上传文件的,下面由学习啦小编为大家整理了linux的ftp下载文件命令的用法的相关知识,希望对大家有帮助! 一.linux中的ftp下载文件命令的用法 从远程 ...

  8. c#通过ftp下载文件

    c#通过ftp下载文件,借用了两位大神的代码  http://www.cnblogs.com/rond/archive/2012/07/30/2611295.html http://blog.csdn ...

  9. Java 实现上传文件到FTP和从FTP下载文件

    在eclipse 中创建项目,并将commons-net-3.6.jar包复制到项目中 commons-net-3.6.jar:链接:https://pan.baidu.com/s/1iz0862TX ...

最新文章

  1. python多值参数函数介绍,数字累加例子
  2. D3.js学习(四)
  3. 使用JMH做Java微基准测试
  4. (素材源码)猫猫学IOS(十)UI之_NSTimer_ios计时器
  5. 数论 欧几里德算法
  6. 容器技术Docker K8s 4 容器编排技术基础-Kubernetes
  7. Java反射机制的大厂面试题
  8. DoIP专栏 - DoIP概述
  9. unity3D禁用脚本
  10. POM文件Unknown报错
  11. 连接共享打印机提示没有权限使用网络资源
  12. 《人生七年》纪录片中问的问题
  13. flume之退避算法backoff algorithm
  14. 准备买笔记本电脑了.
  15. python:求出歌手的得分
  16. codeforces1492 D. Genius‘s Gambit python
  17. 微信——产品设计分析报告
  18. C++实现动态烟花,噼里啪啦过新年啦
  19. Eclipse luna Subversive(SVN) 的安装
  20. linux设置定时任务(crontab)

热门文章

  1. 智能家居实体门店老板如何搭建线上运营体系,实现业绩倍增?
  2. 基于RKE部署的rancher管理平台迁移
  3. Kafka Partition重分配流程简析
  4. 实现抖音哈哈镜效果---OpenCV-Python开发指南(47)
  5. 用java画爱心图_java实现心形图案桃心
  6. 2021北大软微计算机考研感想——从另一角度看考研
  7. Macbook pro下通过docker方式安装kafka
  8. DBeaver使用——excel、csv数据导入中文乱码问题
  9. 直流电机驱动器能量回收
  10. 用ATL创建COM组件详细解说