当前位置:网站首页>ftp服务器搭建部署与C#实现ftp文件的上传
ftp服务器搭建部署与C#实现ftp文件的上传
2022-07-22 03:57:00 【樱花花】
ftp服务器搭建部署与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检测目录是否存在,不存在则创建文件夹,参考以下链接
边栏推荐
猜你喜欢
Les bases de l'IOT - NB
Jd-h5 development
Learning notes of line segment tree
RS+BCH级联编译码误码率性能matlab仿真
Why does the system we developed have concurrent bugs? What is the root cause of concurrent bugs?
m利用SIMILINK仿真模块实现多径信道的动态仿真模拟
第三讲 shell语法
SOC custom IP core -- breathing lamp
Dominoes staged: the beginning and end of the three arrow capital crash
Collagen protease loaded albumin composite nanoparticles / bovine serum albumin coated ceria nano artificial enzyme
随机推荐
Query and association of flask to database
2022那些测试员面试中的“潜规则”,千万不要踩坑....
Chant Developer Workbench 2022
P6327 interval plus interval sin sum
第三讲 shell语法
ENVI_ Idl: batch production of thematic maps
P6327 区间加区间sin和
机器学习基础篇(5)图像轮廓
DOM style operation
Preparation of albumin sorafenib /gd2o3/cus composite albumin nanoparticles /alb-icg-dox albumin nanoparticles coated with ICG & dox
JVM经典面试20问
NB-IOT可以应用在哪些领域
EasyCVR平台设备分组新增以及编辑操作异常的问题修复
Jd-h5 development
Evening Sky
信号FFT、STFT、小波变换、包络分析等
Basic knowledge of nb-iot
M using similink simulation module to realize dynamic simulation of multipath channel
Dominoes staged: the beginning and end of the three arrow capital crash
JVM内存模型:类加载器的分类和获取