1

我有这个源代码:

public static void FTP_SERVER()
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://myurl.com/mainfolder/");
    request.Method = WebRequestMethods.Ftp.ListDirectory;
    request.Credentials = new NetworkCredential("myusername", "mypassword");
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    ArrayList directories = new ArrayList();
    while (!reader.EndOfStream)
    {
        String directory = reader.ReadLine();
        int i = 0;
        for (i = 0; i < directories.Count && Convert.ToInt32(directory) > Convert.ToInt32(directories[i] + ""); i++);
        directories.Insert(i, directory);
    }
    String[] agents = Crawler.CrawlerDbUtils.getAllAgentIDs();

    reader.Close();
    response.Close();
    int j = 0;
    for (int i = 0; i < directories.Count; i++)
    {
        try
        {
            while ((j < agents.Length) && (Convert.ToInt32(agents[j]) < Convert.ToInt32(directories[i] + "")))
            {
                try
                {
                    request = (FtpWebRequest)WebRequest.Create("ftp://myurl.com/mainfolder/" + agents[j]);
                    request.Method = WebRequestMethods.Ftp.MakeDirectory;
                    request.Credentials = new NetworkCredential("myusername", "mypassword");
                    response = (FtpWebResponse)request.GetResponse();
                    responseStream = response.GetResponseStream();
                    response.Close();
                }
                catch (Exception exception)
                { }
                j++;
            }
            if (Convert.ToInt32(agents[j]) == Convert.ToInt32(directories[i] + ""))
            {
                request = (FtpWebRequest)WebRequest.Create("ftp://myurl.com/mainfolder/" + directories[i] + "/");
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.Credentials = new NetworkCredential("myusername", "mypassword");
                response = (FtpWebResponse)request.GetResponse();
                responseStream = response.GetResponseStream();
                reader = new StreamReader(responseStream);
                ArrayList files = new ArrayList();
                while (!reader.EndOfStream)
                {
                    String file = reader.ReadLine();
                    int q = 0;
                    for (q = 0; q < files.Count && file.CompareTo(files[q] + "") > 0; q++) ;
                    files.Insert(q, file);
                }
                reader.Close();
                response.Close();
                String[] dbFiles = Crawler.CrawlerDbUtils.getAllPictures(directories[i] + "");
                int r = 0;
                for (int q = 0; q < files.Count; q++)
                {
                    while ((r < dbFiles.Length) && ((dbFiles[r] + "").CompareTo(files[q] + "") < 0))
                    {
                        r++;
                    }
                    try
                    {
                        if ((r >= dbFiles.Length) || ((dbFiles[r] + "").Equals(files[q]) == false))
                        {
                            request = (FtpWebRequest)WebRequest.Create("ftp://myurl.com/mainfolder/" + directories[i] + "/" + files[q]);
                            request.Method = WebRequestMethods.Ftp.DeleteFile;
                            request.Credentials = new NetworkCredential("myusername", "mypassword");
                            response = (FtpWebResponse)request.GetResponse();
                            responseStream = response.GetResponseStream();
                            response.Close();
                        }
                    }
                    catch (Exception exception)
                    { }
                }
                j++;
            }
            else
            {
                request = (FtpWebRequest)WebRequest.Create("ftp://myurl.com/mainfolder/" + directories[i] + "/");
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.Credentials = new NetworkCredential("myusername", "mypassword");
                response = (FtpWebResponse)request.GetResponse();
                responseStream = response.GetResponseStream();
                reader = new StreamReader(responseStream);
                ArrayList files = new ArrayList();
                while (!reader.EndOfStream)
                {
                    files.Add(reader.ReadLine());
                }
                reader.Close();
                response.Close();
                for (int k = 0; k < files.Count; k++)
                {
                    request = (FtpWebRequest)WebRequest.Create("ftp://myurl.com/mainfolder/" + directories[i] + "/" + files[k]);
                    request.Method = WebRequestMethods.Ftp.DeleteFile;
                    request.Credentials = new NetworkCredential("myusername", "mypassword");
                    response = (FtpWebResponse)request.GetResponse();
                    responseStream = response.GetResponseStream();
                    response.Close();
                }
                request = (FtpWebRequest)WebRequest.Create("ftp://myurl.com/mainfolder/" + directories[i] + "/");
                request.Method = WebRequestMethods.Ftp.RemoveDirectory;
                request.Credentials = new NetworkCredential("myusername", "mypassword");
                response = (FtpWebResponse)request.GetResponse();
                responseStream = response.GetResponseStream();
                response.Close();
            }
        }
        catch (Exception exception)
        {
        }
    }
    while (j < agents.Length)
    {
        try
        {
            request = (FtpWebRequest)WebRequest.Create("ftp://myurl.com/mainfolder/" + agents[j] + "/");
            request.Method = WebRequestMethods.Ftp.MakeDirectory;
            request.Credentials = new NetworkCredential("myusername", "mypassword");
            response = (FtpWebResponse)request.GetResponse();
            responseStream = response.GetResponseStream();
            response.Close();
        }
        catch (Exception exception)
        { }
        j++;
    }
    MessageBox.Show("DONE");

}

它用于从服务器中删除不推荐使用的目录和文件。我有一个主文件夹,其中所有子文件夹都是数字,所有子文件夹都只包含文件(没有子文件夹的子文件夹)

服务器上的文件夹将存储在称为目录的 ArrayList 中。

存储在数据库中的目录 ID 将被加载到称为代理的 String[] 中。

目录会按升序排序,代理也是按升序排序(本项目中都是数字,所以按数字排序)

如果服务器上存在目录,但数据库中不存在,则将其删除(首先删除其所有文件,然后将文件夹本身也删除)

如果一个目录在服务器上不存在,但存在于存储目录列表中,则会在服务器上创建该目录。

如果服务器上和存储目录列表中也存在目录,则不推荐使用的文件将被删除。服务器上相应目录中的文件称为files,而存储在数据库中的文件存储在一个String[]称为dbFiles 中。

该功能本质上是创建要创建的文件夹,删除要删除的文件夹和删除要删除的文件,它可以工作。但是,我注意到在某些情况下发生服务器错误时,索引会回落,我想知道可能是什么原因。

例如 i = 500 并且(我不知道为什么)当 i = 100 是这种情况并且 i 回落到 100 时,会为命令捕获服务器错误。

我的问题是:为什么每当捕获到以前发生的错误时我的索引就会回落?

j 会回到我 100 岁时的状态吗?

如何防止这种回退?

4

3 回答 3

3

您应该重构您的代码,以便 ftp 命令在函数中,而不是在任何地方复制/粘贴这些 ftp 命令,您有一些这样的流程:

String[] directories = ftpGetListing("ftp://myurl.com/mainfolder/");
String[] agents = Crawler.CrawlerDbUtils.getAllAgentIDs();

String[] combinedDirectories = getElementsInBothArray(directories, agents);
String[] serverOnlyDirectories = getElementsOnlyInFirstArray(directories, agents);
String[] agentOnlyDirectories = getElementsOnlyInFirstArray(agents, directories);

// step 1: delete all server only directories
ftpDeleteDirectories("ftp://myurl.com/mainfolder/", serverOnlyDirectories);

// step 2: create all agent only directories
ftpCreateDirectories("ftp://myurl.com/mainfolder/", agentOnlyDirectories);

// step 3: depricate all files
foreach(String dir in combinedDirectories)
{
  String ftpDir = "ftp://myurl.com/mainfolder/" + dir + "/";
  String[] serverFiles = ftpGetListing(ftpDir);
  String[] agentFiles = Crawler.CrawlerDbUtils.getAllPictures(dir);

  String[] serverOnlyFiles = getElementsOnlyInFirstArray(serverFiles, agentFiles);

  foreach(String file in serverOnlyFiles)
  {
    ftpDeleteFile(ftpDir + file);
  }
}

然后,您只需为ftpGetListingftpDeleteDirectoriesftpCreateDirectories和实现非常简单的函数ftpDeleteFile。哦,是的,还有一些简单的数组操作函数,例如:getElementsInBothArraysgetElementsOnlyInFitstArray.

于 2010-09-14T22:03:27.753 回答
1

不是 J 被重置,而是它没有被递增。下面是一个模拟控制流的控制台程序。

控制台的输出会根据 //throw new InvalidOperationException 是否被注释掉而发生很大变化。这在您的 while(!reader.EndOfStream) 附近。

老实说,这个流程非常复杂。如果将这种方法分解成一口大小的碎片,你可能会得到很好的服务。

class Program
{
    static void Main(string[] args)
    {
        int agentCount = 1000;
        int directoriesCount =100;
        int fileCount = 100;
        int dbFilesCount = 100;
        int j = 0;
        for (int i = 0; i < directoriesCount; i++)
        {
            Console.WriteLine("I : {0}", i);
            try
            {
                while ((j < agentCount))
                {
                    try
                    {

                    }
                    catch (Exception exception)
                    { }
                    j++;
                    Console.WriteLine("J : {0}", j);
                }
                if (true)
                {

                    //throw new InvalidOperationException("Some error"); UnComment and see
                    while (false)
                    {

                    }


                    int r = 0;
                    for (int q = 0; q < fileCount; q++)
                    {
                        while ((r < dbFilesCount) )
                        {
                            r++;
                        }
                        try
                        {
                            if ((r >= dbFilesCount))
                            {

                            }
                        }
                        catch (Exception exception)
                        { }
                    }
                    j++;
                    Console.WriteLine("J : {0}", j);
                }
                else
                {

                    while (false)
                    {

                    }

                    for (int k = 0; k < fileCount; k++)
                    {

                    }

                }
            }
            catch (Exception exception)
            {
            }
        }
        while (j < agentCount)
        {
            try
            {

            }
            catch (Exception exception)
            { }
            j++;
            Console.WriteLine("J : {0}", j);
        }
        Console.WriteLine("Done");
        Console.ReadLine();


    }
}
于 2010-09-14T21:53:00.080 回答
1

我已经找到了解决方案。

首先,必须设置此项以避免该问题:

request.KeepAlive = false;

其次,在捕获中必须包括:

response.Close();
responseStream.Close();

当我们打开一个 responseStream 并且只有

response.Close();

当未打开 responseStream 时。

我不确定这个问题的原因,但我认为 C# 正在为我的源代码创建一个分支。

于 2010-09-19T20:11:24.397 回答