How to download FTP files with automatic resume in case of disconnect

北城以北 提交于 2021-02-15 02:16:19

问题


I can download FTP files, but the download code does not have a resume facility and multi part files download. Because there are file larger than 500 MB, I can't download them continuously, because the connection gets closed and it starts download from beginning. I wanted my code a resume facility if it gets disconnected.

The code I am using is:

public string[] GetFileList()
{
    string[] downloadFiles;
    StringBuilder result = new StringBuilder();
    FtpWebRequest reqFTP;
    try
    {
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(
            new Uri("ftp://" + ftpServerIP + "/"));
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
        WebResponse response = reqFTP.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        //MessageBox.Show(reader.ReadToEnd());
        string line = reader.ReadLine();
        while (line != null)
        {
            result.Append(line);
            result.Append("\n");
            line = reader.ReadLine();
        }
        result.Remove(result.ToString().LastIndexOf('\n'), 1);
        reader.Close();
        response.Close();
        //MessageBox.Show(response.StatusDescription);
        return result.ToString().Split('\n');
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
        downloadFiles = null;
        return downloadFiles;
    }
}

private void Download(string filePath, string fileName)
{
    FtpWebRequest reqFTP;
    try
    {
        //filePath = <<The full path where the file is to be created.>>, 
        //fileName = <<Name of the file to be created
        //             (Need not be the name of the file on FTP server).>>
        FileStream outputStream =
            new FileStream(filePath + "\\" + fileName, FileMode.Create);

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(
            new Uri("ftp://" + ftpServerIP + "/" + fileName));
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        Stream ftpStream = response.GetResponseStream();
        long cl = response.ContentLength;
        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);
        }

        ftpStream.Close();
        outputStream.Close();
        response.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

回答1:


Before you start downloading check for the existence of the file on the local filesystem. If it exists, then get the size and use that for the ContentOffset member of the FtpWebRequest object. This functionality may not be supported by the FTP server, though.




回答2:


A native implementation of FTP download resumption using the FtpWebRequest:

bool resume = false;
do
{
    try
    {
        FileMode mode = resume ? FileMode.Append : FileMode.Create;
        resume = false;
        using (Stream fileStream = File.Open(@"C:\local\path\file.dat", mode))
        {
            var url = "ftp://ftp.example.com/remote/path/file.dat";
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
            request.Credentials = new NetworkCredential("username", "password");
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.ContentOffset = fileStream.Position;
            using (Stream ftpStream = request.GetResponse().GetResponseStream())
            {
                ftpStream.CopyTo(fileStream);
            }
        }
    }
    catch (WebException)
    {
        resume = true;
    }
}
while (resume);


来源:https://stackoverflow.com/questions/6331133/how-to-download-ftp-files-with-automatic-resume-in-case-of-disconnect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!