C#:FTP上传缓冲区大小

R.V*_*tor 4 c# ftp io upload buffer

我已经去了FTP上传功能,但有一些我想问的问题是缓冲区大小,我把它设置为20KB是什么意思,如果我增加/减少它会有什么不同吗?

    private void Upload(string filename)
    {
        FileInfo fi = new FileInfo(filename);

        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://" + textBox1.Text + "/" + Path.GetFileName(filename));
        ftp.Credentials = new NetworkCredential(textBox2.Text, textBox3.Text);
        ftp.Method = WebRequestMethods.Ftp.UploadFile;
        ftp.UseBinary = true;
        ftp.KeepAlive = false;
        ftp.ContentLength = fi.Length;

        // The buffer size is set to 20kb
        int buffLength = 20480;
        byte[] buff = new byte[buffLength];
        int contentLen;

        //int totalReadBytesCount = 0;

        FileStream fs = fi.OpenRead();

        try
        {
            // Stream to which the file to be upload is written
            Stream strm = ftp.GetRequestStream();

            // Read from the file stream 2kb at a time
            contentLen = fs.Read(buff, 0, buffLength);

            // Till Stream content ends
            while (contentLen != 0)
            {
                // Write Content from the file stream to the 
                // FTP Upload Stream
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }

            // Close the file stream and the Request Stream
            strm.Close();
            fs.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Upload Error");
        }
    }
Run Code Online (Sandbox Code Playgroud)

Eug*_*its 9

对于桌面系统上的FTP,块大小约为256Kb,在我们的测试中产生了最佳性能.小缓冲区大小会显着降低传输速度.我建议你自己做一些测量,但20Kb对于缓冲区来说肯定是太少了.