C#:FTP上传缓冲区大小 [英] C# : FTP upload buffer size
本文介绍了C#:FTP上传缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经去了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;
//缓冲区大小设置为20kb
int buffLength = 20480;
byte [] buff = new byte [buffLength];
int contentLen;
// int totalReadBytesCount = 0;
FileStream fs = fi.OpenRead();
尝试
{
//要上传文件的流
Stream strm = ftp.GetRequestStream();
//一次从文件流2kb读取
contentLen = fs.Read(buff,0,buffLength);
//直到流内容结束
while(contentLen!= 0)
{
//将文件流中的内容写入
// FTP上传流
strm.Write(buff,0,contentLen);
contentLen = fs.Read(buff,0,buffLength);
}
//关闭文件流和请求流
strm.Close();
fs.Close();
$ b catch(Exception ex)
{
MessageBox.Show(ex.Message,Upload Error);
}
}
解决方案
桌面系统上的FTP大小约为256Kb,在我们的测试中产生了最佳性能。小缓冲区大小会显着降低传输速度。我建议你自己做一些测量,但对于缓冲区来说20Kb肯定太少了。
I have go that FTP Upload function , but there is something that i want to ask about It is the Buffer size , i set it to 20KB what does it mean and would it make difference if i increased/decreased it ?
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");
}
}
解决方案
For FTP on desktop systems block size of about 256Kb produced the best performance in our tests. Small buffer sizes decrease speed of transfer significantly. I recommend that you do some measurements yourself, but 20Kb is definitely too little for a buffer.
这篇关于C#:FTP上传缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文