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)。

相关推荐
码农101号4 小时前
Linux中shell编程表达式和数组讲解
linux·运维·服务器
是小满满满满吗4 小时前
传输层:udp与tcp协议
linux·服务器·网络
安木夕4 小时前
C#-Visual Studio宇宙第一IDE使用实践
前端·c#·.net
小刘同学++4 小时前
ECB(电子密码本,Electronic Codebook) 和 CBC(密码分组链接,Cipher Block Chaining)区分于用途
网络·ssl
Mintimate5 小时前
云服务器 Linux 手动 DD 安装第三方 Linux 发行版:原理与实战
linux·运维·服务器
gadiaola5 小时前
【计算机网络】第3章:传输层—TCP 拥塞控制
网络·网络协议·tcp/ip·计算机网络
猎板PCB厚铜专家大族5 小时前
多层PCB技术解析:从材料选型到制造工艺的深度实践
网络·制造
RussellFans5 小时前
Linux 环境配置
linux·运维·服务器
网硕互联的小客服5 小时前
503 Service Unavailable:服务器暂时无法处理请求,可能是超载或维护中如何处理?
服务器·git·github
高冷的肌肉码喽6 小时前
Linux-进程间的通信
linux·运维·服务器