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
0
投稿
猜你喜欢
- 引言Java届很难得有读百十行代码就能增加修炼的机会,这里有一个。通常,我在看书的时候一般不写代码,因为我的脑袋被设定成单线程的,一旦同时喂
- import java.io.UnsupportedEncodingException;import java.security.Inval
- 这篇文章主要介绍了新手学习微服务SpringCloud项目架构搭建方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 本文主要介绍了25行Java代码将普通图片转换为字符画图片和文本的实现,分享给大家,具体如下:原图生成字符画文本(像素转换字符显示后,打开字
- SpringBoot集成Mybatis时mybatis.mapper-locations和@MapperScan的作用1、mybatis.m
- 因为最近用报表导出比较多,所有就提成了一个工具类,本工具类使用的场景为 根据提供的模板来导出Excel报表并且可根据提供的模板S
- Spring容器可以在不使用<constructor-arg>和<property>元素的情况下自动装配相互协作的b
- 本文实例为大家分享了java web实现简单留言板的具体代码,供大家参考,具体内容如下一、目标用户可以登录并记住密码进入留言板,添加留言,点
- 首先说微信企业号的开发模式分为:编辑模式(普通模式)和开发模式(回调模式) ,在编辑模式下,只能做简单的自定义菜单和自动回复消息,要想实现其
- 前言一般生成的PDF文档默认的文档底色为白色,我们可以通过一定方法来更改文档的背景色,以达到文档美化以及保护双眼的作用。 以下内容提供了Ja
- 前言在RequestMappingHandlerAdapter对request进行了适配,并且调用了目标handler之后,其会返回一个Mo
- Memento定义:memento是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到原先保存的状态。Memento模式相
- 本文为大家分享了JAVA语言课程设计:连连看小游戏,供大家参考,具体内容如下1.设计内容界面中有5*10的界面,图中共有6种不同的图片,每两
- 本文实例讲述了Spring实战之ResourceLoader接口资源加载用法。分享给大家供大家参考,具体如下:一 代码package lee
- 遗传算法是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法。它能解决很多问题,
- 一、问题最近在做代码重构,代码工程采用了Controller/Service/Dao分层架构,Dao层使用了Mybatis-Plus框架。在
- 本文实例讲述了WinForm实现的图片拖拽与缩放功能。分享给大家供大家参考,具体如下:最近做项目的时候遇到上传施工平面布置图,查看,因为图片
- 图片解析:1.生成字节码文件的过程可能产生编译时异常(checked),由字节码文件到在内存中加载、运行类此过程可能产生运行时异常(unch
- java多态性多态分两种:(1) 编译时多态(设计时多态):方法重载。(2) 运行时多态:J
- 背景众所周知,所有被打开的系统资源,比如流、文件或者Socket连接等,都需要被开发者手动关闭,否则随着程序的不断运行,资源泄露将会累积成重