首页 新闻 会员 周边 捐助

【急】高分求C#匿名下载FTPtxt的代码!百度不到

0
悬赏园豆:20 [已解决问题] 解决于 2016-08-11 12:11

如题,假设FTP设定了可以匿名访问根目录下A文件夹,里面有a.txt, 如何写代码?

网上貌似还没有相关资料

王庆东mas的主页 王庆东mas | 初学一级 | 园豆:4
提问于:2016-08-09 13:48
< >
分享
最佳答案
1
        public bool DownloadFile(string localFilePath, string ftpFilePath)
        {
            bool success = false;
            FtpWebRequest ftpWebRequest = null;
            FtpWebResponse ftpWebResponse = null;
            Stream ftpResponseStream = null;
            FileStream outputStream = null;
            
            try
            {
                string dir = Path.GetDirectoryName(localFilePath);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                if (File.Exists(localFilePath))
                {
                    File.Delete(localFilePath);
                }
                
                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFilePath));
                ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);//匿名的话,这里赋值为空
                ftpWebRequest.UseBinary = true;
                ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;

                ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();

                ftpResponseStream = ftpWebResponse.GetResponseStream();
                long contentLength = ftpWebResponse.ContentLength;

                int bufferSize = 2048;
                byte[] buffer = new byte[bufferSize];
                int readCount;

                readCount = ftpResponseStream.Read(buffer, 0, bufferSize); 
                outputStream = new FileStream(localFilePath, FileMode.Create);

                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
                }
                
                success = true;
            }
            catch (Exception e)
            {
                success = false;
                throw;
            }
            finally
            {
                if (outputStream != null)
                {
                    outputStream.Close();
                }

                if (ftpResponseStream != null)
                {
                    ftpResponseStream.Close();
                }

                if (ftpWebResponse != null)
                {
                    ftpWebResponse.Close();
                }
            }

            return success;
        }    

 

收获园豆:20
ArthurLi | 小虾三级 |园豆:686 | 2016-08-09 13:57

Userid还是Password赋值为空?

王庆东mas | 园豆:4 (初学一级) | 2016-08-09 13:58

@0ng_Ching_Tong: username和password都是空值

ArthurLi | 园豆:686 (小虾三级) | 2016-08-09 13:59

@Don'tWorryBeHappy: 你确定?我听人说 userid要填“ANONYMOUS“,密码不填?

王庆东mas | 园豆:4 (初学一级) | 2016-08-09 14:09

@0ng_Ching_Tong: 我这边用都不用填,你可以试试

ArthurLi | 园豆:686 (小虾三级) | 2016-08-09 14:12

@Don'tWorryBeHappy: 好的 我试试先

王庆东mas | 园豆:4 (初学一级) | 2016-08-09 14:12
其他回答(1)
0

直接下载整个文件下来,然后读取就可以了

blog_zt | 园豆:242 (菜鸟二级) | 2016-08-09 13:50

wo也知道啊,代码怎么实现匿名下载ftp文件。。

支持(0) 反对(0) 王庆东mas | 园豆:4 (初学一级) | 2016-08-09 13:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册