随笔 - 149  文章 - 8  评论 - 248  阅读 - 21万

C# 实现FTP上传与下载

向FTP服务器下载文件的简单实例

复制代码
Code
string filePath = "d:\\";
            
string fileName = "lhking.txt"//文件下载之后要保存的路径和文件名
            FtpWebRequest reqFTP;
            
try
            {
                FileStream outputStream 
= new FileStream(filePath +"\\" + fileName, FileMode.Create);
                
string filename = "ip.txt";
                
string ftpServerIP = "222.76.217.24";
                reqFTP 
= (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" +ftpServerIP + "/" + filename));
                reqFTP.Method 
= WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary 
= true;
                reqFTP.Credentials 
= new NetworkCredential("l","l");

                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);
                
while (readCount > 0)
                {
                    outputStream.Write(buffer, 
0, readCount);
                    readCount 
= ftpStream.Read(buffer, 0, bufferSize);
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }
            
catch (Exception err) 
            { 
                MessageBox.Show(err.Message,
"Download Error");
            }
复制代码


向FTP服务器上传文件的简单实例

复制代码
Code
string filename = "ip.txt";
            
string ftpServerIP = "222.76.217.24";
            FileInfo fileInf 
= new FileInfo(filename);
            
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
            FtpWebRequest reqFTP;
            reqFTP 
= (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
            reqFTP.Credentials 
= new NetworkCredential("l","l");
            reqFTP.KeepAlive 
= false;
            reqFTP.Method 
= WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary 
= true;
            reqFTP.ContentLength 
= fileInf.Length;

            
int buffLength = 2048;
            
byte[] buff = new byte[buffLength];
            
int contentLen;
            FileStream fs 
= fileInf.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, 
"Upload Error");
            }
复制代码

如果想把Stream转换为字符串的话,下面这样写就行了。
                StreamReader sr = new StreamReader(ftpStream, Encoding.Default);
                string str = sr.ReadToEnd();

posted on   几度夕阳红了  阅读(10318)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2009年4月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示