1:  /// <summary>
  2:     /// FTP 管理类
  3:     /// </summary>
  4:     public class FTPManage
  5:     {
  6:         private string _Host;         //服务器地址
  7:         private int? _Port;         //端口号
  8:         private string _User;         //用户名
  9:         private string _Pass;         //密码
 10:         //public string  Path;         //具体文件路径
 11:         //public string  fileName;     //具体下载文件
 12:         //public string  SaveFilePath; //保存路径
 13: 
 14:         /// <summary>
 15:         /// 构造函数
 16:         /// </summary>
 17:         /// <param name="Host"></param>
 18:         /// <param name="Port"></param>
 19:         /// <param name="User"></param>
 20:         /// <param name="Pass"></param>
 21:         public FTPManage(string Host, int? Port, string User, string Pass)
 22:         {
 23:             this._Host = Host;
 24:             this._Port = Port;
 25:             this._User = User;
 26:             this._Pass = Pass;
 27:         }
 28: 
 29:         /// <summary>
 30:         /// 判断文件是否存在
 31:         /// </summary>
 32:         /// <returns></returns>
 33:         public string ChkIsFile(string path, string fielName)
 34:         {
 35:             string defaultStr = string.Empty;                   //默认不存在
 36:             if (string.IsNullOrEmpty(fielName)) //必须填写文件名
 37:             {
 38:                 return defaultStr = "文件名不能为空";
 39:             }
 40:             string url = string.Format("ftp://{0}/{1}/{2}", _Host, path, fielName);
 41:             var request = (FtpWebRequest)WebRequest.Create(url);
 42:             request.Credentials = new NetworkCredential(_User, _Pass);
 43:             request.Method = WebRequestMethods.Ftp.GetFileSize;
 44:             try
 45:             {
 46:                 FtpWebResponse response = (FtpWebResponse)request.GetResponse();
 47:             }
 48:             catch (WebException ex)
 49:             {
 50:                 FtpWebResponse response = (FtpWebResponse)ex.Response;
 51:                 if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
 52:                 {
 53: 
 54:                 }
 55:                 return defaultStr = "获取文件异常";
 56:             }
 57:             return defaultStr;
 58:         }
 59: 
 60:         /// <summary>
 61:         /// 下载文件
 62:         /// </summary>
 63:         /// <param name="path"></param>
 64:         /// <param name="file"></param>
 65:         /// <param name="SavePath"></param>
 66:         /// <returns></returns>
 67:         public string LodeFile(string path, string fielName, string SavePath)
 68:         {
 69:             string defaultStr = string.Empty;
 70:             if (string.IsNullOrEmpty(fielName)) //必须填写文件名
 71:             {
 72:                 return defaultStr = "文件名不能为空";
 73:             }
 74:             if (string.IsNullOrEmpty(SavePath)) //必须填写文件名
 75:             {
 76:                 return defaultStr = "文件保存路径不能为空";
 77:             }
 78:             try
 79:             {
 80:                 string url = string.Format("ftp://{0}/{1}/{2}", _Host, path, fielName);
 81:                 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(url);
 82:                 reqFTP.UseBinary = true;
 83:                 reqFTP.Credentials = new NetworkCredential(_User, _Pass);
 84: 
 85:                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
 86:                 Stream ftpStream = response.GetResponseStream();
 87:                 long cl = response.ContentLength;
 88:                 int bufferSize = 2048;
 89:                 int readCount;
 90:                 byte[] buffer = new byte[bufferSize];
 91: 
 92:                 readCount = ftpStream.Read(buffer, 0, bufferSize);
 93:                 FileHelper.IsDirectory(SavePath);
 94:                 FileStream outputStream = new FileStream(SavePath + "\\" + fielName, FileMode.Create);
 95:                 while (readCount > 0)
 96:                 {
 97:                     outputStream.Write(buffer, 0, readCount);
 98:                     readCount = ftpStream.Read(buffer, 0, bufferSize);
 99:                 }
100:                 ftpStream.Close();
101:                 outputStream.Close();
102:                 response.Close();
103:             }
104:             catch (Exception ex)
105:             {
106:                 defaultStr = "文件下载异常:" + ex.Message;
107:             }
108:             return defaultStr;
109:         }
110: 
111:         /// <summary>
112:         /// 获取指定目录文件列表
113:         /// </summary>
114:         /// <returns></returns>
115:         public string[] GetFileList(string Path)
116:         {
117:             try
118:             {
119:                 string url = string.Format("ftp://{0}/{1}", _Host, Path);
120:                 //获得FTP文件上的文件列表 
121:                 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
122:                 request.Method = WebRequestMethods.Ftp.ListDirectory;
123:                 request.Credentials = new NetworkCredential(_User, _Pass);
124: 
125:                 FtpWebResponse response = (FtpWebResponse)request.GetResponse();
126:                 Stream stream = response.GetResponseStream();
127: 
128:                 StreamReader reader = new StreamReader(stream);
129:                 List<string> files = new List<string>();
130: 
131:                 while (!reader.EndOfStream)
132:                 {
133:                     string file = reader.ReadLine();
134:                     files.Add(file);
135:                 }
136: 
137:                 reader.Close();
138:                 stream.Close();
139: 
140:                 return files.ToArray();
141:             }
142:             catch { return new string[0]; }
143:         }
144: 
145:     }

FTP Client的更多相关文章

  1. Ubuntu Filezilla FTP Client 安装

    /************************************************************************************* * Ubuntu File ...

  2. 使用 FileZilla FTP Client连接Vsftpd在执行LIST命令后提示连接超时

    使用 FileZilla FTP Client 连接 Vsftpd在执行LIST命令后提示连接超时. vi /etc/vsftpd/vsftpd.conf 添加: #开启被动模式 pasv_enabl ...

  3. csharp: FTP Client Library using System.Net.FtpWebRequest

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. C# Ftp Client 基本操作

    C# Ftp Client 上传.下载与删除 简单介绍一下Ftp Client 上传.下载与删除,这是目前比较常用的命令,各个方法其实都差不多,重点是了解Ftp命令协议. 1.建立连接 public ...

  5. FileZilla FTP Client

    FileZilla Client是一个快速.实用.多功能和界面直观的免费的FTP客户端,虽然它是免费软件,可功能却一点也不含糊,比起那些共享软件来有过之而无不及,在新的版本中作者改进了手动下载的界面和 ...

  6. csharp: FTP Client Library using System.Net.FtpClient and FluentFTP,测试中存在的BUG修正

    https://netftp.codeplex.com/ /// <summary> /// Gets a file listing from the server. Each FtpLi ...

  7. csharp:FTP Client Library using FtpWebRequest or Sockets

    https://netftp.codeplex.com/SourceControl/latest http://ftplib.codeplex.com/ https://www.codeproject ...

  8. 【FileZilla FTP Client】文件与服务器操作客户端

    跨平台的FTP,FTPS和SFTP客户端 可以断点续传进行上传.下载(需要服务器支持). 自定义命令. 可进行站点管理.

  9. Spring Boot Ftp Client 客户端示例支持断点续传

    本章介绍 Spring Boot 整合 Ftpclient 的示例,支持断点续传 本项目源码下载 1 新建 Spring Boot Maven 示例工程项目 注意:是用来 IDEA 开发工具 File ...

随机推荐

  1. android分享到新浪微博,认证+发送微博,

    分享到新浪微博,折腾了大半个月,现在终于弄出来了,心里的那个爽呀,太痛快了,哈哈!! 废话少说,首先是认证, 1.进入新浪微博提供的开放平台http://open.weibo.com/ 注册新浪账号. ...

  2. [TypeScript] Avoid any type

    To avoid using "any" type is a best pratice. The reason for that is it disable the power o ...

  3. Swift自适应布局(Adaptive Layout)教程(二)

    给TextContainer中添加内容 打开 Main.storyboard ,从组件库(Object Library)中拖拽两个 Label 组件到TextContainer中,位置可以随意摆放: ...

  4. DEV GridControl 导出到Excel

    SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "导出Excel"; sa ...

  5. (转)log4net使用详解

    说明:本程序演示如何利用log4net记录程序日志信息.log4net是一个功能著名的开源日志记录组件.利用log4net可以方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包括M ...

  6. 从blob字段读取图片 在浏览器显示

    public byte[] GetProImg(string JID) { byte[] Buffer = null; using (OracleConnection conn = new Oracl ...

  7. Android-Context的IO功能

    如何将应用数据保存到本地文件?如何从本地文件加载数据到应用中?我实现的步骤是: 应用(java数据)<--org.json-->JSONString<--Context.IO--&g ...

  8. WPF bitmap转bitmapimage 使用 CreateBitmapSourceFromHBitmap内存泄漏

    IntPtr f = bmp.GetHbitmap(); img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitm ...

  9. nginx对于Yii2的前后台的配置

    虽然是个基础问题,但也折腾了不少时间,记录下来,希望对新手有所帮助.例如我的前台地址yooao.cc,后台地址back.yooao.cc  back.yooao.cc是yooao.cc的一个子域名. ...

  10. POJ1636 动态规划+并查集

    POJ1636 问题重述: 两个监狱中各有m个囚犯,欲对这两个监狱中的囚犯进行等数量的交换.已知某些囚犯不能关押在同一个监狱,求解可以交换人数的最大值k (k < m/2). 分析: 假设监狱1 ...