当前位置:网站首页>c#搭建ftp服务器并实现文件上传和下载
c#搭建ftp服务器并实现文件上传和下载
2022-06-25 06:43:00 【ykxxcs】
搭建ftp服务器并实现文件上传和下载
1、建立窗体
2、引入命名空间
windows系统搭建FTP服务器教程,参考下面链接
感谢本文链接作者:https://blog.csdn.net/weixin_43908647/article/details/113140942;
3、具体代码如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//windows系统搭建FTP服务器教程,参考下面链接
//感谢本文链接作者:https://blog.csdn.net/weixin_43908647/article/details/113140942;
string FTPAddress = "ftp://***.***.***.***:21"; //ftp服务器地址
string FTPUsername = "******"; //ftp服务器用户名
string FTPPwd = "******";//ftp服务器密码
//上传文件
private void button1_Click(object sender, EventArgs e)
{
string LocalPath = "d:\\ftp\\a.txt"; //待上传文件
FileInfo f = new FileInfo(LocalPath);
string FileName = f.Name;
string FTPPath = FTPAddress + "/" + FileName; //
//实现文件传输协议 (FTP) 客户端
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPPath));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(FTPUsername, FTPPwd); //设置通信凭据,输入ftp服务器用户名和密码
reqFtp.KeepAlive = false; //请求完成后关闭ftp连接
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();
}
catch (Exception ex)
{
MessageBox.Show("上传失败", ex.Message.ToString());
}
}
//下载文件
private void button2_Click(object sender, EventArgs e)
{
string FtpFilePath = "/a.txt"; //远程路径" "ftp://***.***.***.***:21"; //ftp服务器地址
try
{
string LocalPath = "d:\\ftp\\a.txt"; //下载到的本地路径
MessageBox.Show(LocalPath);
if (File.Exists(LocalPath))//判断需要下载的文件是否存在,
{
File.Delete(LocalPath);//如果文件存在,就删掉
}
string FTPPath = FTPAddress + FtpFilePath;//原创 ,此处为服务器的存放文件路径(FTPAddress) +需要下载的文件名字(FtpFilePath)
//string FTPPath = FTPAddress + "/" + LocalPath;
MessageBox.Show(FTPPath);
//建立ftp连接
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPPath));
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(FTPUsername, FTPPwd);
FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int buffersize = 2048;
int readCount;
byte[] buffer = new byte[buffersize];
readCount = ftpStream.Read(buffer, 0, buffersize);
//创建并写入文件
FileStream OutputStream = new FileStream(LocalPath, FileMode.Create);
while (readCount > 0)
{
OutputStream.Write(buffer, 0, buffersize);
readCount = ftpStream.Read(buffer, 0, buffersize);
}
ftpStream.Close();
OutputStream.Close();
response.Close();
if (File.Exists(LocalPath))
MessageBox.Show("下载完毕");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
边栏推荐
猜你喜欢
洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)
Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus
Importer des données dans MATLAB
将数据导入到MATLAB
Modular programming of LCD1602 LCD controlled by single chip microcomputer
使用Adobe Acrobat Pro调整PDF页面为统一大小
Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test
Anaconda based module installation and precautions
【论文学习】《VQMIVC》
剑指offer刷题(简单等级)
随机推荐
2265. number of nodes with statistical value equal to the average value of subtree
基于RBAC 的SAAS系统权限设计
RMQ区间最大值下标查询,区间最值
2021ICPC网络赛第一场
取消word文档中某些页面的页眉
Atlas conference vulnerability analysis collection
LeetCode_哈希表_中等_454.四数相加 II
[little knowledge] PCB proofing process
50. Pow(x, n)-快速幂
C control refresh
Can bus working condition and signal quality "physical examination"
Mysql面试-执行sql响应比较慢,排查思路。
Force deduction 76 questions, minimum covering string
NSIS silent installation vs2013 runtime
MySQL simple permission management
1742. 盒子中小球的最大数量
Take you through the normalization flow of GaN
Runtime - Methods member variable, cache member variable
差点被这波Handler 面试连环炮带走~
Usememo simulation usecallback