我正在尝试使用C#和ftpwebrequest从ftp服务器下载文件。我可以使用BinaryReader获取字节,但是当我尝试使用br.ReadBytes(int)读取流时,我得到一个BinaryReader不支持搜索操作的错误。
有谁知道如何最好地读取字节,以便我可以将它们写入文件?
这是完整的方法:
public void DownloadFile(String fileName)
{
Logger.Info("starting to download file: " + fileName);
try
{
var downloadFileRequest = (FtpWebRequest)WebRequest.Create(FtpServer + "//" + fileName);
downloadFileRequest.Credentials = new NetworkCredential(FtpUsername,FtpPassword);
downloadFileRequest.Method = WebRequestMethods.Ftp.DownloadFile;
downloadFileRequest.UseBinary = true;
ServicePoint sp = downloadFileRequest.ServicePoint;
sp.ConnectionLimit = 2;
Logger.Info("getting ftp response for download file for " + fileName);
try
{
var downloadResponse = (FtpWebResponse)downloadFileRequest.GetResponse();
Logger.Info("getting ftp response stream for " + fileName);
try
{
Stream downloadStream = downloadResponse.GetResponseStream();
Logger.Info("File Download status: {0}", downloadResponse.StatusDescription.Trim());
Logger.Info("getting binary reader for " + fileName);
try
{
using ( var downloadReader = new BinaryReader(downloadStream))
{
String destinationFilePath= Path.Combine(LocalFolder, fileName);
Logger.Info("writing response stream to " + destinationFilePath);
try
{
using (var downloadWriter = new BinaryWriter(System.IO.File.Open(destinationFilePath, FileMode.Create)))
{
downloadWriter.Write(downloadReader.ReadBytes((int)downloadStream.Length));
}
Logger.Info("successfully saved " + destinationFilePath);
}
catch (Exception ex)
{
Logger.Info("could not save " + destinationFilePath+ " b/c: " + ex.Message);
}
}
}
catch (Exception ex)
{
Logger.Info("could not read " + fileName + " b/c: " + ex.Message);
}
}
catch (Exception ex)
{
Logger.Info("could not open download stream for " + fileName + " b/c: " + ex.Message);
}
finally
{
downloadResponse.Close();
}
}
catch (Exception ex)
{
Logger.Info("could not get ftp response stream for " + fileName + " b/c: " + ex.Message);
}
}
catch (Exception ex)
{
Logger.Info("could not get ftp request stream for " + fileName + " b/c: " + ex.Message);
}
}
这是正在进行的服务的一部分,所以我不想抛出会阻止服务的错误。相反,我正在写日志。以下是此方法的日志内容:
2009-10-07 16:33:29.1421|INFO|xxx.Web.Controllers.FtpController|starting to download file: 2009-10-06155728Z_metadata.txt
2009-10-07 16:33:29.1421|INFO|xxx.Web.Controllers.FtpController|getting ftp response for download file for 2009-10-06155728Z_metadata.txt
2009-10-07 16:33:29.6661|INFO|xxx.Web.Controllers.FtpController|getting ftp response stream for 2009-10-06155728Z_metadata.txt
2009-10-07 16:33:29.6661|INFO|xxx.Web.Controllers.FtpController|File Download status: 125 Data connection already open; Transfer starting.
2009-10-07 16:33:29.6721|INFO|xxx.Web.Controllers.FtpController|getting binary reader for 2009-10-06155728Z_metadata.txt
2009-10-07 16:33:29.6721|INFO|xxx.Web.Controllers.FtpController|writing response stream to C:\\Resumes\\2009-10-06155728Z_metadata.txt
2009-10-07 16:33:29.6951|INFO|xxx.Web.Controllers.FtpController|could not save C:\\Resumes\\2009-10-06155728Z_metadata.txt b/c: This stream does not support seek operations.
我一直在这方面工作太久了,所以任何帮助都会受到赞赏!
谢谢!
答案 0 :(得分:8)
你不应该依赖Stream.Length
,这可能是错误的。您只需要读取while循环中的所有字节,直到没有更多的字节要读取。
MemoryStream ms = new MemoryStream();
byte[] chunk = new byte[4096];
int bytesRead;
while ((bytesRead = downloadStream.Read(chunk, 0, chunk.Length)) > 0)
{
ms.Write(chunk, 0, bytesRead);
}
从那里开始,所有读取的数据都在MemoryStream
中,您可以使用它初始化BinaryReader
。
答案 1 :(得分:1)
Darkassassin的答案很棒!这是我在看到他的帖子之前最终开始工作的代码。它的b / c略有不同,它直接写入文件而不是内存流。
我替换了这一行:
downloadWriter.Write(downloadReader.ReadBytes((int)downloadStream.Length));
用这个:
var buffer = new byte[BufferSize];
int readCount = downloadStream.Read(buffer, 0, BufferSize);
while (readCount > 0)
{
downloadWriter.Write(buffer, 0, readCount);
readCount = downloadStream.Read(buffer, 0, BufferSize);
}
(BufferSize = 4096)
再次感谢您的快速帮助!