为什么我下载的文件是0字节

时间:2014-09-01 11:04:59

标签: asp.net multithreading process

public string GetFilesToMyTemp(string target, FtpWebRequest ftpClient, BBSDbOperations _DbOperations, BBSFtpClient _BBSFtpType)
    {
        FileStream outputStream = null;
        FtpWebResponse response = null;
        Stream ftpStream = null;
        lock (locker)
        {
            try
            {

                using (outputStream = new FileStream(target, FileMode.Create))
                {
                    ftpClient.Method = WebRequestMethods.Ftp.DownloadFile;
                    ftpClient.UseBinary = true;
                    ftpClient.KeepAlive = false;
                    ftpClient.UsePassive = true;
                    response = (FtpWebResponse)ftpClient.GetResponse();

                    ftpStream = response.GetResponseStream();

                    int bufferSize = 2048;

                    int readCount;

                    byte[] buffer = new byte[bufferSize];

                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                    while (readCount > 0)
                    {
                        outputStream.Write(buffer, 0, readCount);
                        readCount = ftpStream.Read(buffer, 0, bufferSize);
                    }
                }
                return "1";
            }
            catch (WebException e)
            {
                String status = ((FtpWebResponse)e.Response).StatusCode.ToString();
                SetIstasyonBilgileriExceptions(_BBSFtpType, e.Message + "-" + status, DateTime.Now, "GetFilesToMyTemp");
                _DbOperations.InsertExceptionsLogs(_BBSFtpType);
                return "0";
            }
            catch (Exception ex)
            {
                //_DbOperations.Logla(_BBSFtpType, "c");
                SetIstasyonBilgileriExceptions(_BBSFtpType, ex.Message, DateTime.Now, "GetFilesToMyTemp");
                _DbOperations.InsertExceptionsLogs(_BBSFtpType);
                return "0";
            }
            finally
            {
                ftpStream.Close();
                ftpStream.Dispose();
                outputStream.Close();
                outputStream.Dispose();
                response.Close();
                response.Dispose();

            }
        }
    }

我使用此方法将ftp服务器中的文件下载到本地目录。此应用程序是多线程的。我有550个文件,这些文件中的10个文件似乎是本地目录中的0个字节。我怎样才能解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

在关闭流之前,您已错过outputStream.Flush();