ftp服务器搭建部署与C#实现ftp文件的上传的示例

Kara ·
更新时间:2024-09-20
· 1808 次阅读

目录

一、简介

二、搭建FTP服务器步骤(Window sserver 2016为例)

三、登录测试  

四、C#上传文件到FTP服务器

一、简介

FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文本协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。在ftp的使用当中,用户经常遇到两个概念:“下载”(Download)和“上传”(upload)。“下载”文件就是从远程主机拷贝文件至自己的计算机上;“上传”文件就是将文件从自己的计算机中拷贝至远程主机上。用Internet语言来说,用户可通过客户机程序向(从)远程主机上传(下载)文件。

二、搭建FTP服务器步骤(Window sserver 2016为例)

安装FTP服务器及部署   

添加FTP站点

IP地址填本机地址(不填则是本机全部IP),端口默认21,SSL是一种数字加密证书,可申请,在此没有可选择无。

 添加ftp上传下载专用用户(也可以选择不添加,使用管理员用户也OK) 

 到此ftp服务器安装和搭建部署,就完成了。

三、登录测试  

浏览器中输入命令 ftp://IP:端口,弹窗提示输入刚刚新建的用户名密码即可。

用户名和密码输入正确的话就会出现公开的路径。

四、C#上传文件到FTP服务器 /// <summary> /// FTP的服务器地址,格式为ftp://192.168.1.234:8021/。 /// </summary> public string FTPCONSTR { get; set; } /// <summary> /// //FTP服务器的用户名 /// </summary> private string FTPUSERID { get; set; } /// <summary> /// //FTP服务器的密码 /// </summary> private string FTPPASSWORD { get; set; } private string ftpIP { get; set; } private string ftpPort { get; set; } public FTPHelper(string ip = "IP", string username = "登录用户名", string password = "用户密码", string port = "端口") { FTPCONSTR = string.Format("{0}://{1}:{2}/", "ftp", ip, port); FTPUSERID = username; FTPPASSWORD = password; } /// <summary> /// 上传文件到远程ftp /// </summary> /// <param name="path">本地的文件目录</param> /// <param name="name">文件名称</param> /// <returns></returns> public bool UploadFile(string path, string name) { FileInfo f = new FileInfo(path); path = FTPCONSTR + name;//这个路径是我要传到ftp目录下的这个目录下 FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path)); reqFtp.Method = WebRequestMethods.Ftp.UploadFile; reqFtp.UsePassive = false;//只需要添加这一句话 reqFtp.UseBinary = true; reqFtp.Credentials = new NetworkCredential(FTPUSERID, FTPPASSWORD); reqFtp.KeepAlive = false; reqFtp.Method = WebRequestMethods.Ftp.UploadFile; reqFtp.ContentLength = f.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; FileStream fs = f.OpenRead(); try { Stream strm = reqFtp.GetRequestStream(); contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } strm.Close(); fs.Close(); return true; } catch (Exception ex) { return false; } }

调用 

string txtFilePath=""; try { OpenFileDialog openFileDialogTemp = new OpenFileDialog(); openFileDialogTemp.Title = "选择要上传的文件"; DialogResult dr = openFileDialogTemp.ShowDialog(); if (!File.Exists(openFileDialogTemp.FileName)) { GLOBALS.msgbox("内容为空,请选择文件"); return; } if (dr == DialogResult.OK) { txtFilePath= openFileDialogTemp.FileName; string filePath = this.txtFilePath.Text; } } catch (Exception ex) { } string id = DateTime.Now.ToString("yyyyMMddHHmmss"); string isPath = DateTime.Now.ToString("yyyy-MM-dd"); string filePath = txtFilePath; string uploadUrl = isPath + "\\" + id + ".jpg"; FTPHelper FTPHelper = new FTPHelper(); bool uploadresult = FTPHelper.UploadFile(filePath, uploadUrl);

如需ftp检测目录是否存在,不存在则创建文件夹,参考以下链接

C# ftp检测目录是否存在和创建文件夹

到此这篇关于ftp服务器搭建部署与C#实现ftp文件的上传的示例的文章就介绍到这了,更多相关C# ftp文件上传内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



ftp ftp服务 示例 C# ftp服务器

需要 登录 后方可回复, 如果你还没有账号请 注册新账号