博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 实现FTP上传与下载
阅读量:6970 次
发布时间:2019-06-27

本文共 2062 字,大约阅读时间需要 6 分钟。

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

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服务器上传文件的简单实例

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");
            }

本文基于署名 GPL 许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名(包含链接),具体操作方式可参考此处。如您有任何疑问或者授权方面的协商,请给我

 

转载于:https://www.cnblogs.com/ljsjxr/p/9274827.html

你可能感兴趣的文章
java集群之session共享解决方案
查看>>
HTML - HTML Commonly Used Character Entities
查看>>
NGUI裁剪模型和粒子
查看>>
hiho_1086_browser_caching
查看>>
绘制图表改变其大小
查看>>
观察者模式
查看>>
利用Nodejs快速构建应用原型
查看>>
【iOS】UITabView/UICollectionView 全选问题
查看>>
Xamarin Android提示内存溢出错误
查看>>
使用Objective-C的文档生成工具:appledoc
查看>>
制作 macOS Sierra 正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)
查看>>
maven install 时提示“程序包 javax.crypto不存在”
查看>>
020医疗项目-模块二:药品目录的导入导出-介绍药品表
查看>>
支持向量机高斯核调参小结
查看>>
【中文分词】二阶隐马尔可夫模型2-HMM
查看>>
UIAlertView/UIAlertController封装使用
查看>>
控制CUP占用率曲线
查看>>
(原)torch中threads的addjob函数使用方法
查看>>
LAMP环境搭建实现网站动静分离[转]
查看>>
云之讯 录音下载
查看>>