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

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

FTP文件上传

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文件下载

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

相关推荐

  1. C#使用ftp进行文件下载功能

    2024-04-20 12:44:03       36 阅读
  2. C++进行FTP下载

    2024-04-20 12:44:03       54 阅读
  3. FTP文件下载

    2024-04-20 12:44:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-20 12:44:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-20 12:44:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-20 12:44:03       82 阅读
  4. Python语言-面向对象

    2024-04-20 12:44:03       91 阅读

热门阅读

  1. 第二十章hive

    2024-04-20 12:44:03       34 阅读
  2. Python网络爬虫项目开发实战:如何处理动态内容

    2024-04-20 12:44:03       40 阅读
  3. React中子传父的方式及原理

    2024-04-20 12:44:03       34 阅读
  4. postgreSQL学习指南(基础)

    2024-04-20 12:44:03       41 阅读
  5. Spring boot注解开发mybatis

    2024-04-20 12:44:03       34 阅读
  6. python-基础(2)-数值运算

    2024-04-20 12:44:03       30 阅读
  7. 基于Python的招聘信息爬虫系统的设计与实现

    2024-04-20 12:44:03       40 阅读
  8. 基于K-prototype算法聚类

    2024-04-20 12:44:03       35 阅读
  9. 【备忘】利用FFMpeg读取视频第一帧作为封面

    2024-04-20 12:44:03       43 阅读
  10. Vue3页面的执行过程

    2024-04-20 12:44:03       38 阅读
  11. unity socket udp 连接

    2024-04-20 12:44:03       187 阅读