如何从 ftp 保存文件?

发布于 2024-12-23 04:11:43 字数 2923 浏览 1 评论 0原文

我有一个将文件夹结构从 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)

I have a methode that copy folder structure from ftp to local folder and then copy all files that consists in them:

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;
}

and the methode that copy current file:

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;
}

But in line Stream responseStream = responseFileDownload.GetResponseStream(); it stop for nearly 40 seconds and then throw an exeption of timeout, and no one file has not been saved (file is small - 50 kb)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

貪欢 2024-12-30 04:11:43

您应该尝试的第一件事是转变被动模式< /a> off 因为大多数防火墙会自动阻止它,但它是 ftpWebRequest 的默认操作模式。

就在这一行下面:

requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

和这一行:

requestFileDownload.UsePassive = false;

The first thing that you should try is to turn passive mode off since this is automatically blocked by most firewalls, but is the default mode of operation for ftpWebRequest.

Just below this line:

requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

and this one:

requestFileDownload.UsePassive = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文