C#使用ftp进行文件上传和下载功能

在C#中,可以使用System.Net命名空间下的FtpWebRequest类来实现FTP文件上传和下载功能。以下是一些示例代码:

FTP文件上传

csharp 复制代码
using System;
using System.IO;
using System.Net;
using System.Text;

public class FtpUploader
{
    public void UploadFile(string localFilePath, string remoteFilePath, string ftpServer, string username, string password)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + "/" + remoteFilePath);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);

        // Convert the file content to a byte array.
        byte[] fileContents = File.ReadAllBytes(localFilePath);

        request.ContentLength = fileContents.Length;

        // Buffer for reading data
        byte[] buffer = new byte[2048];

        // Stream to which the file to be upload is written
        Stream requestStream = request.GetRequestStream();

        // Write Buffer to the File Stream
        requestStream.Write(fileContents, 0, fileContents.Length);
        
        // Close the file stream
        requestStream.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload Complete, status {0}", response.StatusDescription);

        // Close the response.
        response.Close();
    }
}

// 使用示例
class Program
{
    static void Main(string[] args)
    {
        FtpUploader ftpUploader = new FtpUploader();
        string localFilePath = @"C:\path\to\local\file.txt";
        string remoteFilePath = "remote/path/file.txt";
        string ftpServer = "ftp://yourftpserver.com";
        string username = "yourusername";
        string password = "yourpassword";

        ftpUploader.UploadFile(localFilePath, remoteFilePath, ftpServer, username, password);
    }
}

FTP文件下载

csharp 复制代码
using System;
using System.IO;
using System.Net;

public class FtpDownloader
{
    public void DownloadFile(string remoteFilePath, string localFilePath, string ftpServer, string username, string password)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + "/" + remoteFilePath);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential(username, password);

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        FileStream localFileStream = new FileStream(localFilePath, FileMode.Create);

        byte[] buffer = new byte[2048];
        int bytesRead = responseStream.Read(buffer, 0, buffer.Length);

        // Write the downloaded data to a local file.
        while (bytesRead > 0)
        {
            localFileStream.Write(buffer, 0, bytesRead);
            bytesRead = responseStream.Read(buffer, 0, buffer.Length);
        }

        // Close the response and stream objects
        response.Close();
        localFileStream.Close();
    }
}

// 使用示例
class Program
{
    static void Main(string[] args)
    {
        FtpDownloader ftpDownloader = new FtpDownloader();
        string remoteFilePath = "remote/path/file.txt";
        string localFilePath = @"C:\path\to\local\file.txt";
        string ftpServer = "ftp://yourftpserver.com";
        string username = "yourusername";
        string password = "yourpassword";

        ftpDownloader.DownloadFile(remoteFilePath, localFilePath, ftpServer, username, password);
    }
}

请注意,在实际使用中,您可能需要处理异常、日志记录以及更复杂的错误处理。此外,如果您处理大文件,您可能想要使用异步方法以避免阻塞UI线程或主线程。

这些示例仅用于演示目的,并且可能需要针对您的特定需求进行调整。在部署到生产环境之前,请确保代码已经经过充分的测试,并且您已经考虑了安全性问题,比如使用安全的FTP连接(例如SFTP或FTPS)。

相关推荐
群联云防护小杜8 分钟前
深度隐匿源IP:高防+群联AI云防护防绕过实战
运维·服务器·前端·网络·人工智能·网络协议·tcp/ip
van叶~23 分钟前
Linux探秘坊-------15.线程概念与控制
linux·运维·服务器
向宇it28 分钟前
【unity组件介绍】URP Decal Projector贴花投影器,将特定材质(贴花)投影到场景中的其他对象上。
游戏·3d·unity·c#·游戏引擎·材质
2301_780789664 小时前
UDP和TCP的主要区别是什么
服务器·网络协议·web安全·网络安全·udp
_丿丨丨_5 小时前
XSS(跨站脚本攻击)
前端·网络·xss
一只栖枝6 小时前
HCIA-Security 认证精讲!网络安全理论与实战全掌握
网络·web安全·网络安全·智能路由器·hcia·it·hcia-security
FileLink跨网文件交换7 小时前
文件摆渡系统十大软件|文件摆渡系统如何构建网络安全呢?
网络
一个龙的传说7 小时前
linux 常用命令
linux·服务器·zookeeper
斯是 陋室9 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
晨欣9 小时前
大型语言模型(LLM)在网络安全中最具商业价值的应用场景(Grok3 回答 DeepSearch模式)
网络·web安全·语言模型