FTP下载块副本

时间:2012-08-20 13:30:20

标签: c# .net ftp copy

前段时间我问了一个围绕如何将文件从一个位置复制到另一个位置的问题:CopyFileEx "The parameter is invalid" error

我收到了以下代码,非常有帮助。

    static void chunkCopyFile(string source, string destination, int bytesPerChunk)
    {
        uint bytesRead = 0;

        using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read)) {
            using (BinaryReader br = new BinaryReader(fs)) {
                using (FileStream fsDest = new FileStream(destination, FileMode.Create)) {
                    BinaryWriter bw = new BinaryWriter(fsDest);
                    byte[] buffer;

                    for (int i = 0; i < fs.Length; i += bytesPerChunk) {
                        buffer = br.ReadBytes(bytesPerChunk);
                        bw.Write(buffer);
                        bytesRead += Convert.ToUInt32(bytesPerChunk);
                        updateProgress(bytesRead);
                    }
                }
            }
        }
    }

但是,我现在需要将此代码转换为使用FTP。我尝试了将FTP路径传递给文件流的明显问题,但它给了我一个错误,说“不支持”。

我已经设法获取文件长度,我只是不确定如何将下载分成块。任何帮助都会一如既往地受到赞赏!

到目前为止

代码(不多)

static void chunkCopyFTPFile(string destination, int bytesPerChunk)
    {
        uint bytesRead = 0;

        fWR = (FtpWebRequest)WebRequest.Create("ftp://" + FTP_SERVER_NAME + "/test.txt");
        fWR.Method = WebRequestMethods.Ftp.DownloadFile;
        fWR.UseBinary = true;

        fWR.Credentials = new NetworkCredential(FTP_SERVER_USERNAME, FTP_SERVER_PASSWORD);

        FtpWebResponse response = (FtpWebResponse)fWR.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader sR = new StreamReader(responseStream);

        sR.ReadToEnd();

        sR.Close();
        response.Close();
    }

最终代码(工作):

using (Stream responseStream = response.GetResponseStream()) {
            using (BinaryReader bR = new BinaryReader(responseStream)) {
                using (FileStream fsDest = new FileStream(destination, FileMode.Create)) {
                    BinaryWriter bw = new BinaryWriter(fsDest);
                    int readCount;
                    byte[] buffer = new byte[bytesPerChunk];

                    readCount = responseStream.Read(buffer, 0, bytesPerChunk);
                    bytesRead += Convert.ToUInt32(readCount);
                    updateProgress(bytesRead);

                    while (readCount > 0) {
                        bw.Write(buffer, 0, readCount);
                        readCount = responseStream.Read(buffer, 0, bytesPerChunk);
                        bytesRead += Convert.ToUInt32(readCount);
                        updateProgress(bytesRead);
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

好的,这是你可以尝试的。此代码未经过测试。因此,如果它包含错误并且您可以使用它,请编辑答案或指出错误。

static void chunkCopyFile(string source, string destination, int bytesPerChunk)
{
    uint bytesRead = 0;
    //Instead of this:
    //using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read)) {

    //...some necessary stuff...
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    //Use this:
    using(Stream fs = response.GetResponseStream()){

        using (BinaryReader br = new BinaryReader(fs)) {
            using (FileStream fsDest = new FileStream(destination, FileMode.Create)) {
                BinaryWriter bw = new BinaryWriter(fsDest);
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];

                readCount = fs.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                   bw.Write(buffer, 0, readCount);
                   readCount = fs.Read(buffer, 0, bufferSize);
                   updateProgress(readCount);
                }
            }
        }
    }
}

参考文献:

http://msdn.microsoft.com/en-us/library/ms229711

http://www.codeproject.com/Articles/17202/Simple-FTP-demo-application-using-C-Net-2-0(下载方法代码)