C#实现ftp下载功能遇到问题
publicvoidaaa(){intbuflength=10000;byte[]buffer=newbyte[buflength];intreadCount;readC...
public void aaa()
{
int buflength = 10000;
byte[] buffer = new byte[buflength];
int readCount;
readCount = GetFtpFileSize("价格.rar");
int bytesRead = 1;
lstbxFtpState.Items.Add("打开下载通道,文件下载中...");
progressBar1.Maximum = readCount;
progressBar1.Value = 0;
while (bytesRead != 0)
{
//我不太明白while循环里的意思,能帮解释一下嘛,还有下面俩句有没有优先级的说法
filestream.Write(buffer, 0, 10000);
bytesRead = responseStream.Read(buffer, 0, buflength);
Application.DoEvents();
}
progressBar1.Value = readCount;
responseStream.Close();
filestream.Close();
}
} 展开
{
int buflength = 10000;
byte[] buffer = new byte[buflength];
int readCount;
readCount = GetFtpFileSize("价格.rar");
int bytesRead = 1;
lstbxFtpState.Items.Add("打开下载通道,文件下载中...");
progressBar1.Maximum = readCount;
progressBar1.Value = 0;
while (bytesRead != 0)
{
//我不太明白while循环里的意思,能帮解释一下嘛,还有下面俩句有没有优先级的说法
filestream.Write(buffer, 0, 10000);
bytesRead = responseStream.Read(buffer, 0, buflength);
Application.DoEvents();
}
progressBar1.Value = readCount;
responseStream.Close();
filestream.Close();
}
} 展开
1个回答
展开全部
int buflength = 10000;
byte[] buffer = new byte[buflength];
int readCount;
readCount = GetFtpFileSize("价格.rar");
//设置bytesRead=1;为了让你能够进入循环
int bytesRead = 1;
lstbxFtpState.Items.Add("打开下载通道,文件下载中...");
progressBar1.Maximum = readCount;
progressBar1.Value = 0;
while (bytesRead != 0)
{
// FileStream 文件流 将buffer的内容写入文件。但这时的Buffer貌似是空的,所以写入了不到10K的空字节
filestream.Write(buffer, 0, 10000);
// 从报文流读取内容到buffer,读取长度是buflength
bytesRead = responseStream.Read(buffer, 0, buflength);
// 处理当前在消息队列中的所有 Windows 消息,就是移交使用权。最好还是加入延迟Thread.Sleep(),如果当前线程是非主线程的话。
Application.DoEvents();
}
progressBar1.Value = readCount;
responseStream.Close();
filestream.Close();
应该将 filestream.Write(buffer, 0, 10000); 和 bytesRead = responseStream.Read(buffer, 0, buflength); 调换一下。
我将你的循环稍微改了下:
while (bytesRead != 0)
{
// 将内容读取到buffer,buflength 是缓冲大小,bytesRead是实际读取的数量
bytesRead = responseStream.Read(buffer, 0, buflength);
// 判断实际读取数量大于0,才进行写入文件操作
if(bytesRead>0)
{
// 所以在这,写入文件的时候要写入实际读取的数量
filestream.Write(buffer, 0, bytesRead);
}
Application.DoEvents();
}
追问
//现在这个真让我头疼啊,上面的我懂了,但是这个断点续传为什么while循环里往流里写不进去呢老师帮帮我呀,已经单独向你求问了,http://zhidao.baidu.com/question/563433892.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询