C#实现FTP上传文件的方法

作者:農碼一生 时间:2021-11-13 10:46:56 

1.通过用FTP进行上传文件,首先要实现建立FTP连接,一般建立FTP连接,需要知道FTP配置有关的信息。一般要在Bean中建立一个ServiceFileInfo.cs文件进行记录,一般需要FTP地址、登录用户名和登录密码。然后通过其他页面进行访问读取。代码样式如下:

class ServiceFileInfo
   {
       // service1
       public static string txtFilePath = @"ftp://12.128.128.01/FileName/";
       //userid & password
       public static string txtUID = "username";
       public static string txtPWD = "password";
   }

2.通过主方法读取Bean文件下面的的ServiceFileInfo.cs文件的信息,去实现建立FTP连接。这里还需要清楚的知道你上传文件的路径(Path)和文件名称(FileName)。根据这些信息主方法去调用写着Bean中的另外一个ftpOperation.cs 文件(这个.cs文件中主要写一些关于FTP的操作方法),进行FTP访问操作。

  • 主方法调用FTP操作代码

ExecutionResult exeRes = this.ftpOperation.UploadFile(textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt");//.txt为文件的后缀名
  •  Bean文件中ftpOperation.cs文件关于FTP操作的方法

public ExecutionResult UploadFile(string vIMSPath, string vUID, string vPassword, string vLocalPath)
       {
           ExecutionResult result = new ExecutionResult();
           result = connectState(vIMSPath, vUID, vPassword, vLocalPath);//调用下面代码方法
           if (result.Status)
           {
               File.Delete(vLocalPath);
           }
           return result;
       }

connectState()方法

public static ExecutionResult connectState(string vIMSPath, string vUID, string vPassword, string fileName)
       {
           string operater = "";
           bool Flag = false;
           ExecutionResult result;
           result = new ExecutionResult();
           lock (lockObj)
           {
               try
               {
                   operater = "Connet to FTP";
                   FTPOperation ftp = new FTPOperation(new Uri(vIMSPath), vUID, vPassword);
                   operater = "Upload file";
                   Flag = ftp.UploadFile(fileName, Path.GetFileName(fileName), true);
                   if (Flag)
                   {
                       result.Status = true;
                       result.Message = "Send to server OK";
                   }
               }
               catch (Exception ex)
               {
                   result.Status = false;
                   result.Anything = "Mail";
                   result.Message = operater + ":" + ex.Message;
               }
           }
           return result;
       }
  • UploadFile()方法

public bool UploadFile(string LocalFullPath, string RemoteFileName, bool OverWriteRemoteFile)
       {
           bool result;
           try
           {
               bool flag = !this.IsValidFileChars(RemoteFileName) || !this.IsValidFileChars(Path.GetFileName(LocalFullPath)) || !this.IsValidPathChars(Path.GetDirectoryName(LocalFullPath));
               if (flag)
               {
                   throw new Exception("非法文件名或目录名!");
               }
               bool flag2 = File.Exists(LocalFullPath);
               if (!flag2)
               {
                   throw new Exception("本地文件不存在!");
               }
               FileStream fileStream = new FileStream(LocalFullPath, FileMode.Open, FileAccess.Read);
               byte[] array = new byte[fileStream.Length];
               fileStream.Read(array, 0, (int)fileStream.Length);
               fileStream.Close();
               result = this.UploadFile(array, RemoteFileName, OverWriteRemoteFile);
           }
           catch (Exception ex)
           {
               this.ErrorMsg = ex.ToString();
               throw ex;
           }
           return result;
       }

public bool UploadFile(byte[] FileBytes, string RemoteFileName)
       {
           bool flag = !this.IsValidFileChars(RemoteFileName);
           if (flag)
           {
               throw new Exception("非法文件名或目录名!");
           }
           return this.UploadFile(FileBytes, RemoteFileName, false);
       }

public bool UploadFile(byte[] FileBytes, string RemoteFileName, bool OverWriteRemoteFile)
       {
           bool result;
           try
           {
               bool flag = !this.IsValidFileChars(RemoteFileName);
               if (flag)
               {
                   throw new Exception("非法文件名!");
               }
               bool flag2 = !OverWriteRemoteFile && this.FileExist(RemoteFileName);
               if (flag2)
               {
                   throw new Exception("FTP服务上面已经存在同名文件!");
               }
               this.Response = this.Open(new Uri(this.Uri.ToString() + RemoteFileName), "STOR");
               Stream requestStream = this.Request.GetRequestStream();
               MemoryStream memoryStream = new MemoryStream(FileBytes);
               byte[] array = new byte[1024];
               int num = 0;
               for (;;)
               {
                   int num2 = memoryStream.Read(array, 0, array.Length);
                   bool flag3 = num2 == 0;
                   if (flag3)
                   {
                       break;
                   }
                   num += num2;
                   requestStream.Write(array, 0, num2);
               }
               requestStream.Close();
               this.Response = (FtpWebResponse)this.Request.GetResponse();
               memoryStream.Close();
               memoryStream.Dispose();
               FileBytes = null;
               result = true;
           }
           catch (Exception ex)
           {
               this.ErrorMsg = ex.ToString();
               throw ex;
           }
           return result;
       }

来源:https://www.cnblogs.com/wml-it/p/12881936.html

标签:C#,FTP,上传,文件
0
投稿

猜你喜欢

  • 一次Jvm old过高的排查过程实战记录

    2023-05-07 23:33:49
  • 非常实用的侧滑删除控件SwipeLayout

    2023-02-01 14:52:39
  • ShardingSphere jdbc集成多数据源的实现步骤

    2023-11-25 07:54:56
  • 详解Elasticsearch如何实现简单的脚本排序

    2022-03-13 13:17:50
  • Java 转型(向上或向下转型)详解及简单实例

    2021-10-17 14:29:27
  • SpringBoot整合第三方技术的详细步骤

    2023-11-29 08:22:48
  • 将本地的jar包打到Maven的仓库中实例

    2022-08-29 13:38:28
  • 简单谈谈java的异常处理(Try Catch Finally)

    2021-08-01 12:40:02
  • 解决IDEA maven 项目修改代码不生效,mvn clean、install后才生效

    2022-06-19 12:17:06
  • java操作solr实现查询功能的实例

    2023-08-04 10:41:34
  • Java利用自定义注解、反射实现简单BaseDao实例

    2022-09-06 05:29:30
  • 简单了解SpringCloud运行原理

    2023-06-09 17:07:15
  • Springboot Session共享实现原理及代码实例

    2022-12-16 03:29:52
  • 如何解决@NotBlank不生效的问题

    2022-04-01 13:41:08
  • 谈谈为JAXB和response设置编码,解决wechat4j中文乱码的问题

    2023-07-31 01:34:27
  • Java实战在线选课系统的实现流程

    2022-12-19 10:34:30
  • Java实例讲解多态数组的使用

    2021-08-30 19:34:46
  • MyBatis Plus插件机制与执行流程原理分析详解

    2021-10-09 20:53:48
  • Android横竖屏幕切换生命周期详解

    2023-11-02 03:29:45
  • maven创建spark项目的pom.xml文件配置demo

    2023-11-02 01:14:02
  • asp之家 软件编程 m.aspxhome.com