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)对博主进行打赏。】
  • 相关阅读:
    K8s中Secrets
    记一次kubernetes配置secret拉取私仓镜像错误
    K8S中ConfigMap
    阿里云RDSforMySQL如何定位本地IP
    Python3运算符
    nyoj 67-三角形面积 (海伦公式, 叉积)
    nyoj 66-分数拆分 (Java,暴力)
    nyoj 65-另一种阶乘问题 (Java 高精度)
    nyoj 64-鸡兔同笼 (解二元一次方程)
    nyoj 63-小猴子下落 (模拟)
  • 原文地址:https://www.cnblogs.com/studyzy/p/694096.html
Copyright © 2011-2022 走看看