• 使用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)对博主进行打赏。】
  • 相关阅读:
    cocos2dx打包apk
    cocos2d 小游戏
    排序算法笔记二
    把一张合成图分拆出各个小图
    出栈入栈动画demo
    Android 面試題
    AS项目删减打包-01
    c程序指针题
    ubuntu14.04 设置默认登录用户为root
    Ubuntu14.04 Java环境变量配置
  • 原文地址:https://www.cnblogs.com/studyzy/p/694096.html
Copyright © 2020-2023  润新知