Ftp客户端传大文件的问题(在线等待)

zilin82 2006-11-12 07:55:29
我在编写一个Ftp客户端时,下载大文件(象电影)等时,下下来的电影文件文件大小和服务器上的一样大,就是只可以放几分钟,后面的就没有图形。不知道为什么,但是象图形文件、word等几M的文件还是没有问题,希望各位帮忙解决?
源代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.10/crazy.stone.2006.rmvb");
req.Credentials = new NetworkCredential("xzl", "123");
req.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse resp = (FtpWebResponse)req.GetResponse();
long fileSize = resp.ContentLength;
resp.Close();

req = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.10/crazy.stone.2006.rmvb");
req.Credentials = new NetworkCredential("xzl", "123");
req.Method = WebRequestMethods.Ftp.DownloadFile;
resp = (FtpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
FileStream file = new FileStream("e:\\crazy.stone.2006.rmvb" , FileMode.OpenOrCreate);

long offset = 0;
int max = 512;
byte[] data = new byte[max];

while ((fileSize - offset) >= max)
{
stream.Read(data, 0, max);
file.Write(data, 0, max);
offset += max;
}
if ((fileSize - offset) > 0)
{
stream.Read(data, 0, (int)(fileSize - offset));
file.Write(data, 0, (int)(fileSize - offset));
}
stream.Close();
resp.Close();
}
}
}
...全文
361 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mapserver 2006-11-14
  • 打赏
  • 举报
回复
参考这篇文章:
http://www.csharphelp.com/archives/archive9.html
zilin82 2006-11-14
  • 打赏
  • 举报
回复
to Knight94(愚翁) 下下来的文件和原文件一样大,就是播放一段就没有图像,但是时间还在走。
zpingy 2006-11-14
  • 打赏
  • 举报
回复
你把max取值大点试试,有可能是文件内部跨段引起的
Knight94 2006-11-14
  • 打赏
  • 举报
回复
to 还是不行,但是其它大文件可以,象大压缩文件下载后文件没有问题,但是我试了好几个电影文件就不行,原文件没有问题。

文件能下来多少?

你可以参考一下我的文章
http://blog.csdn.net/knight94/archive/2006/08/11/1049393.aspx

zilin82 2006-11-13
  • 打赏
  • 举报
回复
还是不行,但是其它大文件可以,象大压缩文件下载后文件没有问题,但是我试了好几个电影文件就不行,原文件没有问题。
ycqing 2006-11-13
  • 打赏
  • 举报
回复
o
llyzcy 2006-11-13
  • 打赏
  • 举报
回复
学习 UP
zpingy 2006-11-13
  • 打赏
  • 举报
回复
以前见人用过flush,还不知道有此功用
Knight94 2006-11-13
  • 打赏
  • 举报
回复
change
while ((fileSize - offset) >= max)
{
stream.Read(data, 0, max);
file.Write(data, 0, max);
offset += max;
}

with
while ((fileSize - offset) >= max)
{
stream.Read(data, 0, max);
file.Write(data, 0, max);
file.Flush();
offset += max;
}
zilin82 2006-11-13
  • 打赏
  • 举报
回复
怎么没人帮忙,自己顶
liujia_0421 2006-11-12
  • 打赏
  • 举报
回复
也很想研究一下这方面,也借此学习一下...

帮顶了...

111,132

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧