为什么我的System.Net.FtpClient在EnableThreadSafeDataConnections = false时上传不完整的文件?

时间:2016-09-13 16:02:33

标签: c# .net ftp

我有一个将文件FTP到其他服务器的c#程序。我们最近有一个客户端,他们提供了端口21 FTP凭据。我正在使用System.Net.FtpClient上传文件。如果我离开EnableThreadSafeDataConnections = false,则不会完整上传任何文件。最终在FTP站点上的部分文件总是536字节的倍数。

如果我让此属性维护它的默认值为true,则文件会完全上传,但我收到421 Too many connections (8) from this IP错误。这有点道理。我宁愿不断开文件之间的连接,因为有一天我会一次上传30或40个。

以下是一些代码:

            using (var ftpClient = new FtpClient())
            {
                foreach (string Key in LocalAndRemoteFilePathPairs.Keys)
                {
                    if (!File.Exists(Key)) { continue; }

                    ftpClient.Host = Host;
                    ftpClient.Port = fd.PortNumber;
                    ftpClient.Credentials = new NetworkCredential(fd.LoginID, fd.LoginPassword);
                    ftpClient.EnableThreadSafeDataConnections = false; //do not open a new connection for each file
                    ftpClient.DataConnectionType = FtpDataConnectionType.AutoPassive;

                    try
                    {
                        ftpClient.Connect();
                    }
                    catch (Exception conn_ex)
                    {
                        if ("No such host is known" == conn_ex.Message)
                        {
                            /**
                             * Do not fret when we can't connect the FTP,
                             * we might have 
                             * sabotaged the host name or credentials to
                             * specifically avoid uploading files to a 
                             * particular feed host (like localhost!)
                             */

                            Console.WriteLine("Ignoring this exception: [" + conn_ex.Message + "]");
                            return;
                        }
                        ThrowFTPConnectionException(fd, conn_ex);
                    }

                    //If the directory does not exist on the FTP, create it

                        string dir = Path.GetDirectoryName(LocalAndRemoteFilePathPairs[Key]);

                        if ("" != dir)
                        {
                            try
                            {
                                //Does the directory exist?
                                ftpClient.SetWorkingDirectory(dir);

                                /**
                                 * If the previous line didn't throw an 
                                 * exception, the directory exists. Now, 
                                 * get out of it and back where we were.
                                 */
                                foreach (string Level in dir.Split('/'))
                                {
                                    if ("" != Level)
                                    {
                                        ftpClient.SetWorkingDirectory("..");
                                    }
                                }
                            }
                            catch (FtpCommandException)
                            {
                                /** 
                                 * The directory to which we want to upload does not exist.
                                 * dir could actually contain a path of several directories.
                                 */

                                CreateDirectoryRecursively(ftpClient, dir);
                            }
                        }


                    //Actually upload the file
                    using (FileStream fileStream = File.OpenRead(Key))
                    {
                        using (Stream ftpStream = ftpClient.OpenWrite(LocalAndRemoteFilePathPairs[Key]))
                        {
                            Console.WriteLine(
                                "Uploading " + (++counter).ToString() + "/" +
                                LocalAndRemoteFilePathPairs.Count.ToString() +
                                " " + LocalAndRemoteFilePathPairs[Key] +
                                " to " + Host
                            );

                            fileStream.CopyTo(ftpStream);
                        }
                    }

                    ftpClient.Disconnect();
                }
            }

0 个答案:

没有答案