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#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号