【发布时间】:2013-11-16 16:43:44
【问题描述】:
我正在编写自己的 FTPDownload 类,但在更改了一些我试图解决的问题后遇到了一个问题。
我将在下面粘贴的代码有效地开始下载并运行一段时间,在“主”循环的每次迭代中提取完整的数据数组。我可以肯定地说,对于小文件(例如 html 中的会议议程),可以快速且看似完整地复制 - 但是有些东西仍然很不合适。
正如我们所说,我正在下载一个 1.05 gig 文件,我的下载状态控件显示完成了 325 Megs - 我可以证明已经通过流收集了这么多数据但是,这就是事情发生的地方奇怪...到目前为止,我的输出文件中只写入了 548 KB。
以下方法正在收集、计数和写入数据 - 在我最近的更改之前,它的性能相当不错,除了一些性能问题我打算通过将它变成另一个的扩展来解决系列课程。
class DownloadThread : StreamReader
{
protected override void Cycle()
{
if (this.IsComplete)
{
this.FireEvent("OnComplete", new Event(this, "DownloadThread has confirmed completion of the targetted download."));
this.Stop();
return;
}
else if ((this._InputSource = this.GetResponseStream(this.RemotePath)) == null)
{
this.FireEvent("OnException",new Event(this,"DownloadThread could not execute. A Response Stream could not be retrieved from the Input Source '" + this.RemotePath + "'."));
this.Stop();
return;
}
else
{
try
{
this._StartTime = DateTime.Now;
Byte[] Bytes = new Byte[4096];
while ((this.BytesLastRead = this.Read(Bytes, 0, 4096)) > 0)
{
this.LocalStream.Write(Bytes, 0, DataMetrics.Length(Bytes));
this.LocalStream.Flush();
this.BytesSinceLast += this.BytesLastRead;
this.TotalRead += this.BytesLastRead;
this.TriggerProgress();
continue;
}
return;
}
catch (Exception e)
{
this.FireEvent("OnException", new Event(this, "An exception was encountered while reading data for the download." + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace));
return;
}
}
}
protected override Stream GetResponseStream(string RemotePath)
{
this.Request = (FtpWebRequest)FtpWebRequest.Create(this.RemotePath);
if (this.Credentials != null)
this.Request.Credentials = this.Credentials;
this.Response = (FtpWebResponse)this.Request.GetResponse();
Stream ResponseStream = this.Response.GetResponseStream();
return ResponseStream;
}
}
值得注意的是——在任何人得出结论之前——这个“循环”方法会自动在其自己的线程中的循环内运行;并将每 'ThreadDelay' 毫秒重复一次(假设它返回)。
该类中的'LocalStream'变量如下:
protected Stream _LocalStream = null;
protected Stream LocalStream
{
get
{
if (this.LocalFile == null)
return null;
else if (this._LocalStream == null)
{
if (!this.LocalDirectory.Exists)
this.LocalDirectory.Create();
this._LocalStream = this.LocalFile.OpenWrite();
return this.LocalStream;
}
else
return this._LocalStream;
}
}
我认为这可能会导致一些冲突 - 并确保一些线程安全在我的待办事项列表中,但我目前在这个领域没有遇到任何问题 - 我只是假设有人可能想要采取一眼。
根据 'DownloadThread.Read' 方法 - 因为我确定有人会好奇......这是从我自己的 'StreamReader' 类继承的;再次从我的“InputReader”类继承。附属字段包括在下面。
protected Object _InputSource = null;
public Object InputSource
{
get
{
return this._InputSource;
}
}
public Stream InputStream
{
get
{
if (this.InputSource == null)
return null;
else
{
try
{
return (Stream)this.InputSource;
}
catch (Exception)
{
return null;
}
}
}
}
public override double Read(byte[] buffer, int offset, int count)
{
try
{
return this.InputStream.Read(buffer, offset, count);
}
catch (Exception e)
{
this.Stop();
this.FireEvent("OnException", new Events.Event(this, "An exception was encountered while reading from the InputStream." + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace));
return 0;
}
}
现在 - 重申一下 - 我的问题是,由于某种原因,我收到的数据没有被正确地推送到文件中。我即将完成 ~1 gig 下载,因为我正在完成输入,到目前为止没有抛出异常并且流没有“崩溃” - 但我的输出文件只有 17 兆。
再等待 30 秒以完成此下载后,我的 1.05 gig 文件已变成 18 meg 文件;但我看不出为什么上面的代码不能正确处理这个问题。
任何人的建议都会很棒。
【问题讨论】:
-
39 次浏览,但没有一个赞成票...我的问题没有正确形成吗?提供的数据不够吗?我不明白。
-
你可以看到一个来自微软的例子,它更易读、更简单msdn.microsoft.com/en-us/library/ms229711%28v=vs.110%29.aspx
-
我很欣赏这个参考,但这是我最初的代码的来源——这段代码和我的唯一真正的区别是上面提到的执行一个“ReadToEnd”,而我的读取是块,所以我可以更好跟踪下载了多少。重申一下 - 下载一直运行到文件末尾,并且正在提取所有数据,但我的本地文件没有被写入正在读取的完整数据。
-
您使用的是什么技术?
-
您似乎总是写入文件的开头,即在您调用
Stream.Write时偏移量为 0。您能否提供代码的简化版本来说明您尝试修复的行为?
标签: c# ftp stream iostream ftpwebrequest