C# 实现FTP上传资料的示例
作者:農碼一生 时间:2023-10-31 21:19:40
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,上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java字符串比较获取字符串出现次数的示例
2022-03-22 16:05:39
Android实现长按back键退出应用程序的方法
2023-03-26 21:49:43
Java静态泛型使用方法实例解析
2023-03-31 00:41:35
Flutter runApp到渲染上屏分析详解
2023-06-27 12:09:45
![](https://img.aspxhome.com/file/2023/6/106056_0s.jpg)
Java基础学习之方法的重载知识总结
2023-10-27 01:45:15
![](https://img.aspxhome.com/file/2023/2/96132_0s.png)
C#控制台程序如何发布到服务器Linux上运行
2022-07-17 05:57:52
![](https://img.aspxhome.com/file/2023/3/129763_0s.png)
mybatis框架xml下trim中的prefix与suffix等标签的用法
2023-09-20 18:55:24
Java中static变量作用和用法详解
2021-11-09 12:01:04
SpringMVC学习之JSTL条件行为和遍历行为详解
2021-08-19 08:56:09
Android使用SoundPool播放短音效
2022-03-02 12:33:00
Android实现文字上下滚动效果
2023-02-02 07:40:00
MyBatis 如何获取子类的属性
2022-09-12 14:42:39
![](https://img.aspxhome.com/file/2023/0/63650_0s.png)
Java数组高级算法与Arrays类常见操作小结【排序、查找】
2022-12-02 15:17:14
![](https://img.aspxhome.com/file/2023/3/78263_0s.png)
Logback配置文件这么写,还说你不会整理日志?
2022-10-30 10:23:09
![](https://img.aspxhome.com/file/2023/7/105967_0s.png)
C#封装的Sqlite访问类实例
2022-04-28 15:38:13
java实现哈弗曼编码与反编码实例分享(哈弗曼算法)
2023-11-25 04:54:05
Java多线程模拟银行系统存钱问题详解
2023-11-28 22:13:32
![](https://img.aspxhome.com/file/2023/4/60254_0s.jpg)
浅谈android @id和@+id的区别
2021-10-28 06:06:09
Nacos docker单机模式部署实现过程详解
2021-05-27 01:07:28
![](https://img.aspxhome.com/file/2023/9/95939_0s.png)
Spring 自动装配的二义性实例解析
2021-12-16 00:37:55
![](https://img.aspxhome.com/file/2023/5/103865_0s.png)