.NET 文件上传到FTP服务器

本文详细介绍了如何在.NET中利用FtpHelper类实现文件上传到FTP服务器的功能,包括创建文件夹、文件数据统计、目录检查与创建以及文件删除等操作,同时提供了一个FTP文件上传的测试示例。
摘要由CSDN通过智能技术生成

一、背景 

.NET将Web网站中文件上传将文件存储到FTP文件服务器。

二、文件上传FTP代码实现

2.1 FTP文件上传帮助类

  • 文件上传、创建文件夹(多级目录)、文件夹下文件数据统计、文件删除
using Happ.Framework.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

public class FtpHelper
{
    /// <summary>
    /// 文件上传
    /// </summary>
    /// <param name="ftpServer"></param>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <param name="localFilePath"></param>
    /// <param name="remoteFolderPath"></param>
    /// <returns></returns>
    public CommonResult UploadFileWithCreateDirectory(string ftpServer, string username, string password, string localFilePath, string remoteFolderPath)
    {
        CommonResult result = new CommonResult();
        // 创建FTP请求检查目录是否存在
        var checkDirRequest = (FtpWebRequest)WebRequest.Create($"ftp://{ftpServer}/{remoteFolderPath}");
        checkDirRequest.Method = WebRequestMethods.Ftp.ListDirectory;
        checkDirRequest.Credentials = new NetworkCredential(username, password);

        try
        {
            using (var checkDirResponse = (FtpWebResponse)checkDirRequest.GetResponse())
            {
                // 目录存在
                Console.WriteLine("目录已存在: " + remoteFolderPath);
            }
        }
        catch (WebException ex)
        {
            // 如果发生WebException,并且状态码表示目录不存在,则创建目录
            if (ex.Response is FtpWebResponse ftpResponse && ftpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
            {
                CreateFtpDirectory(ftpServer, username, password, remoteFolderPath);
                Console.WriteLine("目录已创建: " + remoteFolderPath);
            }
            else
            {
                throw ex; // 重新抛出异常,可能是其他类型的错误
            }
        }

        // 上传文件
        result = UploadFile(ftpServer, username, password, localFilePath, remoteFolderPath);

        return result;
    }
    /// <summary>
    /// 创建FTP服务器文件夹--单级目录
    /// </summary>
    /// <param name="ftpServer"></param>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <param name="remoteFolderPath"></param>
    private static void CreateFtpDirectory(string ftpServer, string username, string password, string remoteFolderPath)
    {
        var createDirRequest = (FtpWebRequest)WebRequest.Create($"ftp://{ftpServer}/{remoteFolderPath}");
        createDirRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
        createDirRequest.Credentials = new NetworkCredential(username, password);

        using (var createDirResponse = (FtpWebResponse)createDirRequest.GetResponse())
        {
            Console.WriteLine("目录创建成功: " + remoteFolderPath);
        }
    }

  /// <summary>
  /// 创建文件夹:支持创建多层
  /// </summary>
  /// <param name="dirName"></param>
  public   void CreateFtpDirectorys(string ftpServer, string username, string password, string remoteFolderPath)
  {
      FtpWebRequest reqFTP;
      try
      {
          // dirName = name of the directory to create
          String[] realPath = remoteFolderPath.Split('/');
          string currentPath = "/";
          foreach (String path in realPath)
          {
              if (string.IsNullOrEmpty(path) == false)
              {
                  currentPath += $"{path}/";
                  if (!DirectoryExists(ftpServer, username, password, path))
                  {

                      reqFTP = (FtpWebRequest)FtpWebRequest.Create($"ftp://{ftpServer}/{currentPath}");
                      reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                      reqFTP.UseBinary = true;
                      reqFTP.Credentials = new NetworkCredential(username, password);
                      FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                      Stream ftpStream = response.GetResponseStream();
                      ftpStream.Close();
                      response.Close();
                  }
              }

          }
      }
      catch (Exception ex)
      {
          throw new Exception("FtpHelper MakeDir Error --> " + ex.Message);
      }
  }


    /// <summary>
    /// 文件上传
    /// </summary>
    /// <param name="ftpServer"></param>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <param name="localFilePath"></param>
    /// <param name="remoteFolderPath"></param>
    /// <returns></returns>
    public CommonResult UploadFile(string ftpServer, string username, string password, string localFilePath, string remoteFolderPath)
    {
        CommonResult result = new CommonResult();
        result.Success = false;
        var uploadRequest = (FtpWebRequest)WebRequest.Create($"ftp://{ftpServer}/{remoteFolderPath}{Path.GetFileName(localFilePath)}");
        uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
        uploadRequest.Credentials = new NetworkCredential(username, password);
        uploadRequest.UseBinary = true;

        using (var localFileStream = File.OpenRead(localFilePath))
        using (var uploadRequestStream = uploadRequest.GetRequestStream())
        {
            localFileStream.CopyTo(uploadRequestStream);
        }

        using (var uploadResponse = (FtpWebResponse)uploadRequest.GetResponse())
        {
            result.Success = true;

            //Console.WriteLine("文件上传成功: " + localFilePath + " -> " + remoteFolderPath);
        }
        return result;
    }

    /// <summary>
    /// 获取Ftp远程文件的文件数量
    /// </summary>
    /// <param name="ftpServer"></param>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <param name="localFilePath"></param>
    /// <param name="remoteFolderPath"></param>
    /// <returns></returns>
    public List<string> GetFileNames(string ftpServer, string username, string password, string remoteFolderPath)
    {

        // 创建FTP请求
        var request = (FtpWebRequest)WebRequest.Create($"ftp://{ftpServer}/{remoteFolderPath}");
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        request.Credentials = new NetworkCredential(username, password);
        List<string> fileNames = new List<string>();
        try
        {
            // 发送请求并获取响应
            using (var response = (FtpWebResponse)request.GetResponse())
            {
                // 获取响应流并创建StreamReader
                using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    // 读取每一行
                    string line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        // 每行代表一个文件或目录,但我们需要的是文件数,所以忽略目录
                        if (!line.EndsWith("/"))
                        {
                            string[] parts = line.Split(' ');
                            string fileName = parts[parts.Length - 1];
                            fileNames.Add(fileName);

                        }
                    }
                    //Console.WriteLine($"总共有 {fileNames.Count} 个文件");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return fileNames;
    }
    /// <summary>
    /// 删除文件
    /// </summary>
    /// <param name="ftpServer"></param>
    /// <param name="username"></param>
    /// <param name="password"></param>
    /// <param name="fileNames"></param>
    /// <param name="remoteFolderPath"></param>
    /// <returns></returns>
    public bool DeleteFile(string ftpServer, string username, string password, string[] fileNames, string remoteFolderPath)
    {
        bool result = false;    
        try
        {  
            foreach (string fileName in fileNames)
            {
                // 创建一个新的FTP请求
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + ftpServer + remoteFolderPath + "/" + fileName));

                // 设置请求的认证信息
                request.Credentials = new NetworkCredential(username, password);

                // 设置请求的方法为DELE,表示删除文件
                request.Method = WebRequestMethods.Ftp.DeleteFile;

                // 发送请求
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                // 关闭响应流和请求
                response.Close();
                request.Abort();
            }
            result = true;
        }
        catch (Exception ex)
        {

            throw;
        }
        return result;
    }
}

2.2 返回结果实体(可自定义)

 
public class CommonResult
  {
      public bool Success { get; set; }

      public string ErrorMessage { get; set; }

      public string Data1 { get; set; }

      public string Data2 { get; set; }

      public string Data3 { get; set; }

      public string Data4 { get; set; }

      public object Data5 { get; set; }

      public bool Cancel { get; set; }

      public CommonResult()
          : this(null)
      {
      }

      public CommonResult(string errorMessage)
      {
          ErrorMessage = errorMessage;
          Cancel = false;
      }

      public void CopyValue(CommonResult result)
      {
          Cancel = result.Cancel;
          Data1 = result.Data1;
          Data2 = result.Data2;
          Data3 = result.Data3;
          Data4 = result.Data4;
          Data5 = result.Data5;
          Success = result.Success;
          ErrorMessage = result.ErrorMessage;
      }
  }

2.3 FTP文件上传测试

 [TestMethod]
 public void TestFtpFile()
 {
     
     string ftpServer = "192.xx.xx.xx"; 
     string username = "FTP-User"; 
     string password = "*****";
     string localFilePath = "E:\\测试报告.xls";
     string fileName = "测试报告.xls";
     string remoteFolderPath = @"/dev/";
     //1/文件上传

     var res = new FtpHelper().UploadFileWithCreateDirectory(ftpServer, username, password, localFilePath, remoteFolderPath);

     //2.文件夹下文件数量获取

     string[] filesNames =    new FtpHelper().GetFileNames(ftpServer, username, password, remoteFolderPath).ToArray();

     //3、删除文件

    var resdel = new FtpHelper().DeleteFile(ftpServer, username, password, filesNames, remoteFolderPath);


 }

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是一个使用Java上传文件到FTP服务器的示例代码: ```java import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class FTPUploader { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "your-username"; String password = "your-password"; File fileToUpload = new File("path/to/file.txt"); // 要上传的文件路径 FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(username, password); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); FileInputStream fileInputStream = new FileInputStream(fileToUpload); String remoteFile = "uploaded-file.txt"; // 远程服务器上保存的文件名 boolean uploaded = ftpClient.storeFile(remoteFile, fileInputStream); fileInputStream.close(); if (uploaded) { System.out.println("文件上传成功!"); } else { System.out.println("文件上传失败!"); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } } } ``` 以上代码使用了Apache Commons Net库来处理FTP相关操作。你需要将`server`、`port`、`username`和`password`替换为你的FTP服务器的相关信息,将`fileToUpload`替换为你要上传的文件路径,`remoteFile`替换为在服务器上保存的文件名。 请确保你的项目中包含了Apache Commons Net库的依赖。你可以在Maven项目中添加以下依赖: ```xml <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.8.0</version> </dependency> ``` 希望对你有所帮助!如果有任何问题,请随时问我。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Best_to_Own

你的鼓励,是我继续创作的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值