我有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");
}
}
对于桌面系统上的 FTP,大约 256Kb 的块大小在我们的测试中产生了最佳性能。小缓冲区大小会显着降低传输速度。我建议您自己进行一些测量,但 20Kb 对于缓冲区来说绝对太小了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)