C# 实现FTP上传资料的示例
作者:農碼一生 发布时间:2023-10-31 21:19:40
标签:c#,ftp,上传
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


猜你喜欢
- 之前做 Ble 开发都是在 Android 6.0 系统以下的版本中进行测试的,今天使用 Android 6.0 的设备测试的时候,发现扫描
- TCP实现TCP协议需要在双方之间建立连接,通过输入输出流来进行数据的交换,建立需要通过三次握手,断开需要四次挥手,保证了数据的完整性,但传
- 到现在为止,笔者不敢给流下定义,从概念来讲他应该也是一种数据元素才是。可是在我们前面的代码例子中我们可以看到他更多的好像在表示他是一组处理数
- 前言 OFD是国家标准版式文档格式,于2016年生效。OFD文档国家标准参见《电子文件存储与交换格式版式文档》。既然是国家标准,O
- 一、定义1、T 代表一种类型可以加在类上,也可以加在方法上1)T 加在类上class SuperClass<A>{//todo}
- 一、淘宝商品详情页效果我们的效果二、实现思路 使用两个scrollView,两个scroll
- 定义:当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知它的依赖对象。特
- 一、分析 本次博客,主要解决文件上传等一系列问题,将从两方面来论述,
- Android中实现定时器的四种方式第一种方式利用Timer和TimerTask1、继承关系java.util.Timer基本方法sched
- HashMap的实现原理首先有一个每个元素都是链表(可能表述不准确)的数组,当添加一个元素(key-value)时,就首先计算元素key的h
- 需求基于MTK8163 8.1平台定制导航栏部分,在左边增加音量减,右边增加音量加思路需求开始做之前,一定要研读SystemUI Navig
- 1、背景最近用到了Spring Cloud Alibaba开发微服务,在开发的过程中发现,当我们的服务上线或下线的时候,我们的Spring
- 同步代码块基本语句synchronized (任意对象) {操作共享代码}代码示例public class SellTicket imple
- 本文同时讨论了IComparable和IComparer接口,原因有两点。这两个接口经常一起使用。虽然接口类似且名称相似,但它们却有不同的用
- 1.可能是缓存导致的。解决方法:清除缓存!2.全局编译可能项目依赖别的模块,别的模块修改未进行编译,这时须先对依赖模块进行编译补充知识:ID
- 一、EPL II 格式及打印测试注意N命令前的换行和最后P1后的换行。将此段代码复制到windows记事本里另存为Print.ext,文件名
- 方案一.使用国内的镜像阿里仓库等首先通过maven的路径找到setting.xml的文件然后在其中修改mirror和profile保存一下就
- 在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Ac
- 过去在Android上网络通信都是使用的Xutils 因为用它可以顺道处理了图片和网络这两个方面,后来发觉Xutils里面使用的是HttpC
- 一、DataSource首先大家要清楚DataSource属于MyBatis三层架构设计的基础层 然后我们来看看具体的实现。在数据持久层中