zoukankan      html  css  js  c++  java
  • 使用VS2005自己的类库实现FTP下载

    以前上传音乐下载文件等使用的FTP都是到SourceForge上去找的,现在好了,VS2005对WebRequest进行了扩展,除了以前使用的Http类以外还多了FtpWebRequest。现在我们就可以不用第三方的FTP类库了。

    FtpWebRequest实现下载文件的方法如下:

    Stream stream = null;
            StreamReader reader = null;
            try
            {
                FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftp://202.115.22.138/test.txt);
                ftpRequest.Credentials = new NetworkCredential("sa", "studyzy");
                ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                stream = ftpResponse.GetResponseStream();
                reader = new StreamReader(stream, Encoding.Default);
                string txt = reader.ReadToEnd();
                return txt;
            }
            catch
            {
                return "读取FTP文件失败";
            }
            finally
            {
                if (reader != null) { reader.Close(); }
                if (stream != null) { stream.Close(); }
            }

    以上代码只是将FTP中的文本文件读取到内存中,要保存到硬盘只需要使用StreamWriter  。同样的方法可以实现FTP的其他功能。

    【本文章出自博客园深蓝居,转载请注明作者出处,如果您觉得博主的文章对您有很大帮助,欢迎支付宝(studyzy@163.com)对博主进行打赏。】
  • 相关阅读:
    嵌入式系统的应用
    linux shell编程
    JS高级学习历程-1
    JavaScript入门
    二叉树 数据结构
    用css固定textarea文本域大小尺寸
    ie img 3px bug
    OpenCV-Python(1)在Python中使用OpenCV进行人脸检测
    教你用Python解决非平衡数据问题(附代码)
    图片人脸检测(OpenCV版)
  • 原文地址:https://www.cnblogs.com/studyzy/p/694096.html
Copyright © 2011-2022 走看看