FTP程序开发相关类
作者:追风剑情 发布于:2024-12-22 17:55 分类:C#
FtpWebRequest类
FtpWebRequest类用于实现文件传输协议(FTP)客户端的操作,包括文件的删除、上传、下载等功能。表 6-1 列出了 FtpWebRequest 类的一些重要方法。
表 6-1 FtpWebRequest类的重要方法 | |
方法 | 说明 |
Abort | 如果正在进行文件传输,用此方法来终止传输;如果没有进行任何操作,此方法不产生任何作用 |
Create | 初始化新的 WebRequest 对象 |
CreateDefault | 为指定的 URI 方案初始化新的 WebRequest 实例(从 WebRequest 继承) |
GetRequestStream | 检索用于向 FTP 服务器上传数据的流 |
GetResponse | 返回 FTP 服务器响应 |
为了实现 FTP 功能的一般过程,先用 FtpWebRequest 的 Create 方法得到 FtpWebRequest 的实例对象,指向 FTP 服务器的路径。该方法有两种重载形式:
(1)FtpWebRequest.Create(String uriString)。
(2)FtpWebRequest.Create(URI uri)。
例如:
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"+Server+"/"+ fileInf.Name));
如果 FTP 服务器不允许匿名访问,客户端必须向服务器提供用户名和密码(利用 NetworkCredential 类提供给服务器)。
在创建好新的 FtpWebRequest 对象后,要设置 FTP 的执行方法以及该类对象的属性 FtpWebRequest 的属性用来配置应用程序与 FTP 服务器之间的信息。从表 6-2 中可以了解 FtpWebRequest 的一些重要属性。
表 6-2 FtpWebRequest类的重要属性 | |
属性 | 说明 |
Credentials | 获取或设置用于与FTP服务器通信的凭据 |
KeepAlive | 获取或设置一个Boolean值,该值指定在请求完成之后是否关闭到FTP服务器的控制连接(默认值为true) |
Method | 获取或设置要发送到FTP服务器的命令 |
RenameTo | 获取或设置重命名文件的新名称 |
Timeout | 获取或设置等待请求的毫秒数 |
UseBinary | 获取或设置一个Boolean值,该值指定文件传输的数据类型。若要传输文本数据,请将UseBinary属性由默认值(true)更改为false |
UsePassive | 获取或设置客户端应用程序的数据传输过程的行为 |
&emsp Method 属性指定当前请求是什么命令(upload、download、filelist 等)。这个值定义在结构体 WebRequestMethods.Ftp 中。WebRequestMethods.Ftp的公共属性如表 6-3 所示。
表 6-3 WebRequestMethods.Ftp类的重要公共属性 | |
属性 | 说明 |
DeleteFile | 从FTP服务器上删除文件 |
DownloadFile | 从FTP服务器上下载文件 |
ListDirectory | 获取FTP服务器上的文件简短列表 |
ListDirectoryDetails | 获取FTP服务器上的文件详细列表 |
MakeDirectory | 在FTP服务器上创建目录 |
RemoveDirectory | 在FTP服务器上删除目录 |
UploadFile | 向FTP服务器上传文件 |
新建一个 FtpWebRequest 对象,并且初始化该对象。假设 URI 为 "ftp://"+Server+"/"+fileInf.Name,一般通过下面的代码来设置 FtpWebRequest 对象的属性:
FtpWebRequest req;
//根据之前的 URI 创建 FtpWebRequest 对象
req = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + Server + "/" + fileInf.Name));
//提供 FTP 用户名和密码
req.Credentials = new NetworkCredential(UserName, UserPwd);
req.KeepAlive = false;
//指定所要执行的 FTP 指令,假设现在为上传文件的操作
req.Method = WebRequestMethods.Ftp.UploadFile;
//指定传输的数据类型
req.UseBinary = true;
//让 FTP 服务器预知上传文件的大小
req.ContentLength = fileInf.Length;
FtpWebResponse类
, FtpWebResponse类用于封装文件传输协议(FTP)服务器对请求的响应,该类提供操作的状态以及从服务器下载的所有数据设置信息。
获取FTP响应时,需要通过 FtpWebRequest 对象的 GetResponse 方法获取,该方法可以获取 FtpWebResponse 类的对象的实例。当使用时,返回的对象必须强制转换成 FtpWebResponse。当不再需要 FtpWebResponse 对象时,调用 Close 方法释放其所占有的资源。
例如,创建一个 FtpWebResponse 类的实例,代码如下:
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpUriString); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); ...
GetResponse 方法建立控制连接,还可能创建数据连接。该方法在接收到响应之前一直处于阻塞状态。
表 6-4 列出了 FtpWebResponse 类的常用方法。
表 6-4 FtpWebResponse类的重要方法 | |
方法 | 说明 |
Close | 释放响应所持有的资源 |
GetResponseStream | 检索包含从FTP服务器上发送的响应数据的流 |
同样地,可以通过表 6-5 了解 FtpWebResponse 类的常用属性。
表 6-5 FtpWebResponse类的重要属性 | |
属性 | 说明 |
BannerMessage | 获取在登录前建立连接时FTP服务器发送的消息 |
ContentLength | 获取从FTP服务器上接收的数据的长度 |
ContentType | 获取或设置接收的数据的内容类型 |
ExitMessage | 获取FTP会话结束时服务器发送的消息 |
LastModified | 获取FTP服务器上的文件的上次修改日期和时间 |
ResponseUri | 获取对请求发送响应的URI |
StatusCode | 获取从FTP服务器上发送的最新状态码 |
StatusDescription | 获取描述从FTP服务器发送的状态代码的文本 |
NetworkCredential类
NetworkCredential类用于为密码的身份验证方案提供凭据。该类可用于多种协议。在FTP中,该类用于提供FTP用户名和密码。例如:
NetworkCredential networkCredential = new NetworkCredential("用户名", "密码");
表 6-6 列出了 NetworkCredential 类的重要属性。
表 6-6 NetworkCredential类的重要属性 | |
属性 | 说明 |
UserName | 获取或设置与凭据关联的用户名 |
Password | 获取或设置与凭据关联的用户密码 |
Domain | 获取或设置验证凭据的域名或计算机名 |
using System; using System.Net; using System.IO; using System.Text; using System.Threading; using System.Diagnostics; using System.Security.Cryptography.X509Certificates; using UnityDebug = UnityEngine.Debug; /// <summary> /// FTP辅助类 /// </summary> public sealed class FtpHelper { // 打印耗时 private static void PrintTimeConsuming(Stopwatch watch) { long seconds = watch.ElapsedMilliseconds / 1000; if (seconds < 60) { UnityDebug.LogFormat("耗时: {0}秒", seconds); } else { long minutes = seconds / 60; seconds = seconds % 60; UnityDebug.LogFormat("耗时: {0}分{1}秒", minutes, seconds); } } // 计算百分比 [0, 1] public static double NormalPercent(long size, long totalSize) { double percent = (double)size / (double)totalSize; percent = Math.Round(percent, 2);//保留两位小数 return percent; } // 格式化为百分比形式 public static string FormatPercent(long size, long totalSize) { double percent = (double)size / (double)totalSize; percent = Math.Round(percent, 2);//保留两位小数 return string.Format("{0}%", percent * 100); } // 格式化大小显示 public static string FormatSize(long size) { int KB = 1024; int M = KB * 1024; if (size < KB) return string.Format("{0}byte", size); if (size < M) return string.Format("{0}kb", size / KB); return string.Format("{0}M", size / M); } public static string FormatSize(long size, long totalSize) { int KB = 1024; int M = KB * 1024; return string.Format("({0}/{1}M)", size / M, totalSize / M); } // 证书验证方法 private static bool MyCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { // 这里应实现你的证书验证逻辑 // 示例中仅检查是否有错误(生产环境需要更严格的验证) return sslPolicyErrors == System.Net.Security.SslPolicyErrors.None; } // 创建 FtpWebRequest public static FtpWebRequest Create(string uriString, string userName, string password, string method) { //创建Ftp请求对象 FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(new Uri(uriString)); //获得与服务器通信的凭据 req.Credentials = new NetworkCredential(userName, password); //启用SSL安全连接 //如果未启用FTP服务器可能返回: //WebException: The remote server returned an error: (530) 530 Not logged in, need secure connection. req.EnableSsl = true; //接受所有证书(仅测试用) ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, errors) => true; req.KeepAlive = false; //指定要执行的命令 (WebRequestMethods.Ftp) req.Method = method; //指定数据传输类型为二进制 req.UseBinary = true; return req; } // 获取文件大小 public static long GetFileSize(string serverIP, string userName, string password, string downFileName) { string uriString = string.Format("ftp://{0}/{1}", serverIP, downFileName); FtpWebRequest req = Create(uriString, userName, password, WebRequestMethods.Ftp.GetFileSize); using (FtpWebResponse response = (FtpWebResponse)req.GetResponse()) { return response.ContentLength; } } // 上传文件 public static async void UploadFile(string serverIP, string userName, string password, string filePath, Action<string> completedFun=null, Action<string, string> errorFun=null, Action<long, long, double> progressFun=null) { FileInfo fileInfo = new FileInfo(filePath); string uriString = string.Format("ftp://{0}/{1}", serverIP, fileInfo.Name); FtpWebRequest req = Create(uriString, userName, password, WebRequestMethods.Ftp.UploadFile); //文件大小 req.ContentLength = fileInfo.Length; //缓冲区大小 int buffLen = 1024 * 1024; byte[] buff = new byte[buffLen]; int contentLen; //创建文件流 FileStream fs = fileInfo.OpenRead(); //获取上传流 Stream stream = req.GetRequestStream(); long uploadLength = 0; long fileLength = fileInfo.Length; double percent = 0; Stopwatch sw = Stopwatch.StartNew(); contentLen = await fs.ReadAsync(buff, 0, buffLen); while (contentLen != 0) { uploadLength += contentLen; //将内容从 File Stream 写入 Upload Stream stream.Write(buff, 0, contentLen); contentLen = await fs.ReadAsync(buff, 0, buffLen); //进度回调 percent = NormalPercent(uploadLength, fileLength); progressFun?.Invoke(uploadLength, fileLength, percent); } stream.Close(); fs.Close(); sw.Stop(); PrintTimeConsuming(sw); completedFun?.Invoke(filePath); } // 下载文件 public static async void DownloadFile(string serverIP, string userName, string password, string downFileName, string saveFilePath, Action<string> completedFun = null, Action<string, string> errorFun = null, Action<long, long, double> progressFun = null) { string uriString = string.Format("ftp://{0}/{1}", serverIP, downFileName); FtpWebRequest req = Create(uriString, userName, password, WebRequestMethods.Ftp.DownloadFile); //创建输出文件流 FileStream outputStream = new FileStream(saveFilePath, FileMode.Create); //同步请求 FtpWebResponse response = (FtpWebResponse)req.GetResponse(); Stream ftpStream = response.GetResponseStream(); int readCount; long downloadLength = 0; long fileLength = response.ContentLength; double percent = 0; //如果FTP协议不支持返回文件大小,则请求文件大小 if (fileLength == -1) { fileLength = GetFileSize(serverIP, userName, password, downFileName); } //缓冲区大小 int buffLen = 1024 * 1024; byte[] buffer = new byte[buffLen]; //异步方法 Stopwatch sw = Stopwatch.StartNew(); readCount = await ftpStream.ReadAsync(buffer, 0, buffer.Length); while (readCount > 0) { downloadLength += readCount; outputStream.Write(buffer, 0, readCount); readCount = await ftpStream.ReadAsync(buffer, 0, buffer.Length); percent = NormalPercent(downloadLength, fileLength); progressFun?.Invoke(downloadLength, fileLength, percent); } //关闭所有流 ftpStream.Close(); outputStream.Close(); response.Close(); sw.Stop(); PrintTimeConsuming(sw); completedFun?.Invoke(saveFilePath); } // 浏览文件 public static void BrowseFile(string serverIP, string userName, string password, string subFolder="", Action<string[]> completedFun = null) { string uriString = string.Format("ftp://{0}/{1}", serverIP, subFolder); UnityDebug.LogFormat("{0}, Method=ListDirectory", uriString); FtpWebRequest req = Create(uriString, userName, password, WebRequestMethods.Ftp.ListDirectory); string[] list = new string[0]; SynchronizationContext syncContext = SynchronizationContext.Current; //异步请求 IAsyncResult asyncResult = req.BeginGetResponse(new AsyncCallback( ar => { if (!ar.IsCompleted) return; var req = ar.AsyncState as FtpWebRequest; FtpWebResponse response = (FtpWebResponse)req.EndGetResponse(ar); Stream responseStream = response.GetResponseStream(); using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8)) { // 读取所有内容并按行分割 string directoryListing = reader.ReadToEnd(); list = directoryListing.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); UnityDebug.Log(directoryListing); } //在调用此接口的线程执行回调函数 syncContext?.Post(state => { completedFun?.Invoke(list); }, null); }), req); } }
标签: C#
日历
最新文章
随机文章
热门文章
分类
存档
- 2025年6月(1)
- 2025年5月(1)
- 2025年4月(5)
- 2025年3月(4)
- 2025年2月(3)
- 2025年1月(1)
- 2024年12月(5)
- 2024年11月(5)
- 2024年10月(5)
- 2024年9月(3)
- 2024年8月(3)
- 2024年7月(11)
- 2024年6月(3)
- 2024年5月(9)
- 2024年4月(10)
- 2024年3月(11)
- 2024年2月(24)
- 2024年1月(12)
- 2023年12月(3)
- 2023年11月(9)
- 2023年10月(7)
- 2023年9月(2)
- 2023年8月(7)
- 2023年7月(9)
- 2023年6月(6)
- 2023年5月(7)
- 2023年4月(11)
- 2023年3月(6)
- 2023年2月(11)
- 2023年1月(8)
- 2022年12月(2)
- 2022年11月(4)
- 2022年10月(10)
- 2022年9月(2)
- 2022年8月(13)
- 2022年7月(7)
- 2022年6月(11)
- 2022年5月(18)
- 2022年4月(29)
- 2022年3月(5)
- 2022年2月(6)
- 2022年1月(8)
- 2021年12月(5)
- 2021年11月(3)
- 2021年10月(4)
- 2021年9月(9)
- 2021年8月(14)
- 2021年7月(8)
- 2021年6月(5)
- 2021年5月(2)
- 2021年4月(3)
- 2021年3月(7)
- 2021年2月(2)
- 2021年1月(8)
- 2020年12月(7)
- 2020年11月(2)
- 2020年10月(6)
- 2020年9月(9)
- 2020年8月(10)
- 2020年7月(9)
- 2020年6月(18)
- 2020年5月(4)
- 2020年4月(25)
- 2020年3月(38)
- 2020年1月(21)
- 2019年12月(13)
- 2019年11月(29)
- 2019年10月(44)
- 2019年9月(17)
- 2019年8月(18)
- 2019年7月(25)
- 2019年6月(25)
- 2019年5月(17)
- 2019年4月(10)
- 2019年3月(36)
- 2019年2月(35)
- 2019年1月(28)
- 2018年12月(30)
- 2018年11月(22)
- 2018年10月(4)
- 2018年9月(7)
- 2018年8月(13)
- 2018年7月(13)
- 2018年6月(6)
- 2018年5月(5)
- 2018年4月(13)
- 2018年3月(5)
- 2018年2月(3)
- 2018年1月(8)
- 2017年12月(35)
- 2017年11月(17)
- 2017年10月(16)
- 2017年9月(17)
- 2017年8月(20)
- 2017年7月(34)
- 2017年6月(17)
- 2017年5月(15)
- 2017年4月(32)
- 2017年3月(8)
- 2017年2月(2)
- 2017年1月(5)
- 2016年12月(14)
- 2016年11月(26)
- 2016年10月(12)
- 2016年9月(25)
- 2016年8月(32)
- 2016年7月(14)
- 2016年6月(21)
- 2016年5月(17)
- 2016年4月(13)
- 2016年3月(8)
- 2016年2月(8)
- 2016年1月(18)
- 2015年12月(13)
- 2015年11月(15)
- 2015年10月(12)
- 2015年9月(18)
- 2015年8月(21)
- 2015年7月(35)
- 2015年6月(13)
- 2015年5月(9)
- 2015年4月(4)
- 2015年3月(5)
- 2015年2月(4)
- 2015年1月(13)
- 2014年12月(7)
- 2014年11月(5)
- 2014年10月(4)
- 2014年9月(8)
- 2014年8月(16)
- 2014年7月(26)
- 2014年6月(22)
- 2014年5月(28)
- 2014年4月(15)
友情链接
- Unity官网
- Unity圣典
- Unity在线手册
- Unity中文手册(圣典)
- Unity官方中文论坛
- Unity游戏蛮牛用户文档
- Unity下载存档
- Unity引擎源码下载
- Unity服务
- Unity Ads
- wiki.unity3d
- Visual Studio Code官网
- SenseAR开发文档
- MSDN
- C# 参考
- C# 编程指南
- .NET Framework类库
- .NET 文档
- .NET 开发
- WPF官方文档
- uLua
- xLua
- SharpZipLib
- Protobuf-net
- Protobuf.js
- OpenSSL
- OPEN CASCADE
- JSON
- MessagePack
- C在线工具
- 游戏蛮牛
- GreenVPN
- 聚合数据
- 热云
- 融云
- 腾讯云
- 腾讯开放平台
- 腾讯游戏服务
- 腾讯游戏开发者平台
- 腾讯课堂
- 微信开放平台
- 腾讯实时音视频
- 腾讯即时通信IM
- 微信公众平台技术文档
- 白鹭引擎官网
- 白鹭引擎开放平台
- 白鹭引擎开发文档
- FairyGUI编辑器
- PureMVC-TypeScript
- 讯飞开放平台
- 亲加通讯云
- Cygwin
- Mono开发者联盟
- Scut游戏服务器引擎
- KBEngine游戏服务器引擎
- Photon游戏服务器引擎
- 码云
- SharpSvn
- 腾讯bugly
- 4399原创平台
- 开源中国
- Firebase
- Firebase-Admob-Unity
- google-services-unity
- Firebase SDK for Unity
- Google-Firebase-SDK
- AppsFlyer SDK
- android-repository
- CQASO
- Facebook开发者平台
- gradle下载
- GradleBuildTool下载
- Android Developers
- Google中国开发者
- AndroidDevTools
- Android社区
- Android开发工具
- Google Play Games Services
- Google商店
- Google APIs for Android
- 金钱豹VPN
- TouchSense SDK
- MakeHuman
- Online RSA Key Converter
- Windows UWP应用
- Visual Studio For Unity
- Open CASCADE Technology
- 慕课网
- 阿里云服务器ECS
- 在线免费文字转语音系统
- AI Studio
- 网云穿
- 百度网盘开放平台
- 迅捷画图
- 菜鸟工具
- [CSDN] 程序员研修院
- 华为人脸识别
- 百度AR导航导览SDK
- 海康威视官网
- 海康开放平台
- 海康SDK下载
- git download
- Open CASCADE
- CascadeStudio
交流QQ群
-
Flash游戏设计: 86184192
Unity游戏设计: 171855449
游戏设计订阅号
