无法读取从ftp服务器下载的文件的内容 [英] could not read the contents of file downloaded from ftp server

查看:114
本文介绍了无法读取从ftp服务器下载的文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace DTSys.FTPDownloader
{
    public class FileDownloader
    {
        static void Main(string[] args)
        {
            string ftpServer = "ftp:*******.com";
            string userName = "*********.com";
            string password = "********";
            
            string destionationLocation = @"D:\project\ss\";
            try
            {
                FileDownloader fDownloader = new FileDownloader();        
                   List<string> collectionOfFiles = fDownloader.getFileList(ftpServer, userName, password);
                    if (collectionOfFiles != null)
                    {
                        Console.WriteLine("if statement");
                        Console.WriteLine(collectionOfFiles);
                        foreach (string fileName in collectionOfFiles)
                        {                                                   
                            Console.WriteLine("for statment");
                            string fullPathDestionation = Path.Combine(destionationLocation, Path.GetFileName(fileName));
                            fDownloader.downloadFile(ftpServer, fileName, userName, password, fullPathDestionation);
                        }
                        Console.WriteLine("if statement closed");
                    }
                    else
                    {
                        Console.WriteLine("function is not called");
                    }
                
                Console.WriteLine("downloaded successfully");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }        
        public List<string> getFileList(string FTPAddress, string username, string password)
        {
            List<string> files = new List<string>();
            Console.WriteLine("getting filelist");
            try
            {              
                FtpWebRequest request = FtpWebRequest.Create(FTPAddress) as FtpWebRequest;
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.Credentials = new NetworkCredential(username, password);
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = false;
                FtpWebResponse response = request.GetResponse() as FtpWebResponse;
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        while (!reader.EndOfStream)
                        {
                            files.Add(reader.ReadLine());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("files list got");
            return files;            
        }
        public void downloadFile(string FTPAddress, string filename, string username, string password, string destFile)
        {
            Console.WriteLine("Downloading files");
            try
            {                
                FtpWebRequest request = FtpWebRequest.Create(FTPAddress + "/"+filename) as FtpWebRequest;
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.Credentials = new NetworkCredential(username, password);
                request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
                request.UsePassive = true;
                request.UseBinary = false;
                request.KeepAlive = false;                
                FtpWebResponse response = request.GetResponse() as FtpWebResponse;
                Stream reader = response.GetResponseStream();
                byte[] buffer = new byte[1024];                
                DateTime mod = response.LastModified;
                DateTime obj = DateTime.Today;
                int res1 = DateTime.Compare(mod, obj);                
                if (res1 != -1)
                {                    
                    Console.WriteLine("DOWNLOAD IF STATEMENT");
                    using (Stream streamFile = File.Create(destFile))
                    {
                        Console.WriteLine("USING STATEMENT");
                        while (true)
                        {
                            Console.WriteLine("WHILE STATEMENT");
                            int bytesRead = reader.Read(buffer, 0, buffer.Length);
                            if (bytesRead != 0)
                            {
                                Console.WriteLine("WHILE IF STATEMENT");
                                streamFile.Write(buffer, 0, bytesRead);
                            }
                            else
                            {
                                Console.WriteLine("WHILE ELSE STATEMENT");
                                break;
                            }
                        }
                    }
                }                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("downloaded");
        }
    }
}


在上面的代码中,我可以下载使用当前日期创建的文件...但是无法下载文件的内容...因为我将bytesRead的值读取为0 ...但其中包含内容该文件...我不知道为什么...有人可以帮助我吗?


In the above code I can download the files which are created with current date... but the contents of files could not be downloaded... Because I''m getting the value of bytesRead as 0... but there is content in that file... I don''t know why... anybody can help me?

推荐答案

您的主要问题是;
Your main problem is;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;


如果您删除最后修改的检查,就不能设置两次请求方法,就像这样;


You can''t set the request method twice, if you remove the checks for last modified so that is it like;

FtpWebRequest request = FtpWebRequest.Create(FTPAddress + "/" + filename) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = false;
request.KeepAlive = false;
FtpWebResponse response = request.GetResponse() as FtpWebResponse;
Stream reader = response.GetResponseStream();
byte[] buffer = new byte[1024];


它应该下载文件.


It should download the file.


这篇关于无法读取从ftp服务器下载的文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆