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

相关推荐
不灭锦鲤2 小时前
ssrf学习(ctfhub靶场)
网络·学习·安全
weixin_548444262 小时前
2024年最新版本神马TV8.5影视APP源码 293TV影视点播系统源码搭建教程 神马TV8.2加强版反编译教程 保姆级小白可搭建 完整版本视频教程
网络
枫叶丹44 小时前
【在Linux世界中追寻伟大的One Piece】进程信号
linux·运维·服务器
网络研究院4 小时前
如何安全地大规模部署 GenAI 应用程序
网络·人工智能·安全·ai·部署·观点
limengshi1383924 小时前
通信工程学习:什么是RIP路由信息协议
网络·网络协议·学习·智能路由器·信息与通信
灯火不休ᝰ5 小时前
[win7] win7系统的下载及在虚拟机中详细安装过程(附有下载文件)
linux·运维·服务器
数云界8 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
limengshi1383928 小时前
通信工程学习:什么是TFTP简单文件传输协议
网络·网络协议·学习·信息与通信
叫我龙翔10 小时前
【Linux】进程间关系与守护进程
linux·运维·服务器·计算机网络
麻辣韭菜10 小时前
网络基础 【HTTP】
网络·c++·http