按修改日期批量下载 - 505文件不可用

时间:2016-02-01 09:43:56

标签: c# .net ftp ftpwebrequest

我尝试创建一个脚本,按照修改日期从FTP服务器下载多个文本文档,但不超过一天:

 if (_datemodified > DateTime.Now.AddDays(-2));

但是,当我运行我的代码时,它会设法下载200-300个文件,然后超时

  

" System.Net.WebException:远程服务器返回错误:(550)   文件不可用(例如,找不到文件,无法访问)。"

这是错误指向的代码片段:

   static void Checkdatemodified(string url, string savePath)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
        request.Method = WebRequestMethods.Ftp.GetDateTimestamp;

        request.Credentials = new NetworkCredential("user", "******");
        request.UseBinary = true;
        DateTime temp;
        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
        {
            temp = response.LastModified;
        }

        FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create(url);
        request2.Credentials = new NetworkCredential("user", "******");
        request2.Method = WebRequestMethods.Ftp.DownloadFile;
        request2.UseBinary = true;

        if (temp > DateTime.Now.AddDays(-2))
        {

            using (FtpWebResponse response2 = (FtpWebResponse)request2.GetResponse())
            {
                //   DateTime temp = response.LastModified;
                using (Stream rs = response2.GetResponseStream())
                {
                    using (FileStream ws = new FileStream(savePath, FileMode.Create))
                    {
                        byte[] buffer = new byte[2048];
                        int bytesRead = rs.Read(buffer, 0, buffer.Length);

                        while (bytesRead > 0)
                        {
                            ws.Write(buffer, 0, bytesRead);
                            bytesRead = rs.Read(buffer, 0, buffer.Length);

                        }
                    }
                    //      DateTime temp = Checkdatemodified(url);
                    System.IO.File.SetLastWriteTime(savePath, temp);
                }

            }
        }

这是调用函数的代码

            //Create new FTPClient in memory
            using (var ftpClient = new FtpClient())
            {
                // Sets the location of the FTP folder we will use in the request
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://(ftp folder link)");
                //Set the method it will use to list the items in the directory
                request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

                // Set the credentials used to get onto the FTP
                request.Credentials = new NetworkCredential("user", "******");
                //Processes request
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                //Stream reader reads each character of a line 
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);

                //Creates new list of strings and a new string variable
                List<string> lines = new List<string>();
                List<string> lines2 = new List<string>();
                string line;

                //For each line the streamreader reads
                //Split the line into an array and take the last entry and add this to the list
                while ((line = reader.ReadLine()) != null)
                {
                    string[] temp = line.Split(null);
                    string temp2 = temp[temp.Length - 1];
                    lines.Add(temp2);
                }
                //Close everything
                responseStream.Close();
                reader.Close();

                //For each each line in the list starting at number 2(the first 2 are just the directory names)
                for (int i = 2; i < lines.Count - 1; i++)
                {
                    //Set temp strings. 1 for the file path + file name and the other for the location to download to
                    string temp = "ftp://(ftp folder)" + lines[i];
                    string temp2 = @"\\(server folder location)" + lines[i];

                    //If the date modified in the FTP between now and 2 days old then download the fil
                    if (System.IO.File.Exists(temp2))

                    {
                        System.IO.File.Delete(temp2);
                    }
                    Checkdatemodified(temp, temp2);
                    //File.AppendAllText(@"C:\TestFolder\error.txt", value);
                }


               //     DownloadFtpFile(temp, temp2);



            }
    }

我觉得问题是我每次下载使用3个不同的FtpWebRequest来处理300多个文件

  • 1)获取目录列表
  • 2)查找修改日期
  • 3)下载文件

或者它可能是包含文件名的列表的问题。

有谁知道为什么这不起作用?或者,如果有更好的方法通过datemodified批量下载ftp文件

P.S - 代码有点混乱,需要清理,但我只是试图让它先行动

0 个答案:

没有答案