簡體   English   中英

如何將文件從一個ftp移動到另一個

[英]how to move files from one ftp to another

我需要將文件從一個ftp移動到另一個(當前使用ftpwebrequest),都需要身份驗證,並且設置不同(超時,ascii,活動等)。 將文件從一個文件下載到本地服務器,然后再上傳到另一個服務器,這比僅復制文件要慢得多(如果該文件甚至存在,您將如何處理,重命名為?)。 感覺它應該更快,但是我不確定,我對文件復制或下載不了解。

它們都是.txt或.csv文件,每個文件的大小都在3到10 mb左右,因此相當多的數據

您可以使用FXP將文件從FTP服務器A復制到FTP服務器B。 服務器和客戶端都必須支持該功能。

一段時間我們需要從FTP服務器上載文件。 這是C#中FTP操作的一些好例子。 您可以使用它。 這將幫助您制作一個C#程序來完全滿足您的要求。

從FTP服務器下載文件

public void DownloadFile(stringHostURL, string UserName, string Password, stringSourceDirectory, string FileName, string LocalDirectory)
        {
            if(!File.Exists(LocalDirectory + FileName))
            {
                try
                {
                    FtpWebRequestrequestFileDownload = (FtpWebRequest)WebRequest.Create(HostURL + “/” + SourceDirectory + “/” + FileName);
                    requestFileDownload.Credentials = new NetworkCredential(UserName, Password);
                    requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
                    FtpWebResponseresponseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
                    StreamresponseStream = responseFileDownload.GetResponseStream();
                    FileStreamwriteStream = new FileStream(LocalDirectory + FileName, FileMode.Create);
                    intLength = 2048;
                    Byte[] buffer = new Byte[Length];
                    intbytesRead = 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;
                }
                catch(Exception ex)
                {
                    throwex;
                }
            }
        }

一些很好的例子

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM