c# 使用FluentFTP进行ftp下载文件
首先在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下载文件相关推荐
- C#FTP下载文件出现远程服务器返回错误: (500) 语法错误,无法识别命令
如果下载多个文件的时候,有时候莫名其妙的出现500服务器错误,很有可能是没有设置KeepAlive 属性导致的. 出现应用程序未处理的异常:2015/1/6 11:40:56 异常类型:WebExce ...
- 使用FTP下载文件connect.retrieveFileStream(filename) 获取不到InputStream流,返回null的问题
使用同事的代码做FTP下载文件,InputStream in = connect.retrieveFileStream(fileName);执行这句时InputStream总是获取为空 后来把代码改成 ...
- Java实现FTP下载文件到客户端(浏览器)
目录 一.简介 二.maven依赖 三.配置类 四.工具类 4.1.服务器文件名中文处理 4.2.下载文件名中文处理 五.接口验证 一.简介 我在之前的文章(Java实现文件上传和下载)里讲过非F ...
- 【踩坑】Linux java中ftp下载文件,解压文件损坏,以及图片下载打开只显示下载路径的问题
[踩坑]Linux java中ftp下载文件,解压文件损坏,以及图片下载打开只显示下载路径的问题 一. 问题重现 二. 问题解决思路 1. 确认是不是上传就导致数据出错了 2. 是不是平台问题 三. ...
- 从ftp下载文件(word)到本地显示文件损坏或错误
今天遇到问题从ftp下载文件到本地路径无法查看,发现是文件名称中含有比如空格,问号等非常规字符存在,虽然在本地创建了对应的文件,但是流无法识别一些非常规字符,所以即使下载到本地也显示文件损坏或错误.我 ...
- 使用FTP下载文件资源
使用FTP下载文件(新手向) 1.FTP介绍 FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文件传输协议".用于Internet上的 ...
- linux中如何用ftp命令下载文件,linux中ftp下载文件命令的用法
linxu下的ftp命令是用来下载文件或者上传文件的,下面由学习啦小编为大家整理了linux的ftp下载文件命令的用法的相关知识,希望对大家有帮助! 一.linux中的ftp下载文件命令的用法 从远程 ...
- c#通过ftp下载文件
c#通过ftp下载文件,借用了两位大神的代码 http://www.cnblogs.com/rond/archive/2012/07/30/2611295.html http://blog.csdn ...
- Java 实现上传文件到FTP和从FTP下载文件
在eclipse 中创建项目,并将commons-net-3.6.jar包复制到项目中 commons-net-3.6.jar:链接:https://pan.baidu.com/s/1iz0862TX ...
最新文章
- python多值参数函数介绍,数字累加例子
- D3.js学习(四)
- 使用JMH做Java微基准测试
- (素材源码)猫猫学IOS(十)UI之_NSTimer_ios计时器
- 数论 欧几里德算法
- 容器技术Docker K8s 4 容器编排技术基础-Kubernetes
- Java反射机制的大厂面试题
- DoIP专栏 - DoIP概述
- unity3D禁用脚本
- POM文件Unknown报错
- 连接共享打印机提示没有权限使用网络资源
- 《人生七年》纪录片中问的问题
- flume之退避算法backoff algorithm
- 准备买笔记本电脑了.
- python:求出歌手的得分
- codeforces1492 D. Genius‘s Gambit python
- 微信——产品设计分析报告
- C++实现动态烟花,噼里啪啦过新年啦
- Eclipse luna Subversive(SVN) 的安装
- linux设置定时任务(crontab)