如何从ftp保存文件?

问题描述 投票:3回答:1

我有一个方法将文件夹结构从ftp复制到本地文件夹,然后复制包含在其中的所有文件:

public void CreateDirectories()
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Url);
    request.Credentials = new NetworkCredential(Login, Pass);
    request.Method = WebRequestMethods.Ftp.ListDirectory;

    string soursePath = @"L:\Test";

    StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());

    string directoryName = streamReader.ReadLine();

    while (directoryName != null)
    {
        //Create directories structure
        if (directoryName.StartsWith("I") && !directoryName.Contains("p"))
        {
            string newPath = System.IO.Path.Combine(soursePath, directoryName);
            if (!System.IO.Directory.Exists(newPath))
            {
                System.IO.Directory.CreateDirectory(newPath);

                //get file list and invoke DownLoad(string directoryName, string fileName)
                FtpWebRequest fileRequest = (FtpWebRequest)WebRequest.Create(Url + directoryName + "/");
                fileRequest.Credentials = new NetworkCredential(Login, Pass);
                fileRequest.Method = WebRequestMethods.Ftp.ListDirectory;

                StreamReader fileStreamReader = new StreamReader(fileRequest.GetResponse().GetResponseStream());
                string fileName = fileStreamReader.ReadLine();
                while (fileName != null)
                {
                    DownLoad(directoryName, fileName);
                    fileName = streamReader.ReadLine();
                }

            }
        }

        directoryName = streamReader.ReadLine();
    }

    request = null;
    streamReader = null;
}

以及复制当前文件的方法:

public void DownLoad(string directoryName, string fileName)
{
    string localPath = @"L:\Test\" + directoryName;

    FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create("ftp://ftp.equip.me/prod/" + directoryName + "/" + fileName);
    requestFileDownload.Credentials = new NetworkCredential(Login, Pass);
    requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

    FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();

    Stream responseStream = responseFileDownload.GetResponseStream();
    FileStream writeStream = new FileStream(localPath + "\\" + fileName, FileMode.Create);

    int Length = 2048;
    Byte[] buffer = new Byte[Length];
    int bytesRead = responseStream.Read(buffer, 0, Length);

    while (bytesRead > 0)
    {
        writeStream.Write(buffer, 0, bytesRead);
        bytesRead = responseStream.Read(buffer, 0, Length);
    }

    responseStream.Close();
    writeStream.Close();

    requestFileDownload = null;
    responseFileDownload = null;
}

但是在行Stream responseStream = responseFileDownload.GetResponseStream();它停止了将近40秒,然后抛出一个超时的例子,没有一个文件没有保存(文件很小 - 50 kb)

c# .net ftp httpwebrequest
1个回答
0
投票

你应该尝试的第一件事是关闭passive mode,因为它被大多数防火墙自动阻止,但是ftpWebRequest的默认操作模式。

就在这条线下面:

requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

还有这个:

requestFileDownload.UsePassive = false;
© www.soinside.com 2019 - 2024. All rights reserved.