0

ファイルを FTP ダウンロードする方法がありますが、ファイルをローカルに保存するのではなく、ftp 応答を介してメモリ内のファイルを解析します。私の質問は、ftp 応答ストリームを取得した後にストリーム リーダーを返すことは良い習慣ですか? 同じメソッドで解析やその他のことをしたくないからです。

var uri = new Uri(string.Format("ftp://{0}/{1}/{2}", "somevalue",     remotefolderpath, remotefilename));
var request = (FtpWebRequest)FtpWebRequest.Create(uri);
request.Credentials = new NetworkCredential(userName, password);

request.Method = WebRequestMethods.Ftp.DownloadFile;

var ftpResponse = (FtpWebResponse)request.GetResponse();
/* Get the FTP Server's Response Stream */
ftpStream = ftpResponse.GetResponseStream();
return responseStream = new StreamReader(ftpStream);
4

3 に答える 3

2

For me there are 2 disadvantages of using the stream directly, if you can live with them, you shouldn't waste memory or disk space.

  • In this stream you can not seek to a specific position, you can only read the contents as it comes in;
  • Your internet connection could suddenly drop and you will get an exception while parsing and processing your file, either split the parsing and processing or make sure your processing routine can handle the case that a file is processed for a second time (after a failure halfway through the first attempt).

To work around these issues, you could copy the stream to a MemoryStream:

using (var ftpStream = ftpResponse.GetResponseStream())    
{
   var memoryStream = new MemoryStream()
   while ((bytesRead = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
   {
      memoryStream.Write(buffer, 0, bytesRead);
   }  

   memoryStream.Flush();
   memoryStream.Position = 0;
   return memoryStream;                   
}

If you are working with larger files I prefer writing it to a file, this way you minimize the memory footprint of your application:

using (var ftpStream = ftpResponse.GetResponseStream())    
{
   var fileStream = new FileStream(Path.GetTempFileName(), FileMode.CreateNew)
   while ((bytesRead = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
   {
      fileStream.Write(buffer, 0, bytesRead);
   }  

   fileStream.Flush();      
   fileStream.Position = 0;
   return fileStream;
}
于 2013-06-26T09:24:50.263 に答える
0

カルロスに感謝します。本当に役に立ちました。byte[] を返すだけです

           byte[] buffer = new byte[16 * 1024];

           using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
               memoryStream=ms;
            }
         return memoryStream.ToArray();

このようなメソッドで byte[] を使用しました

   public async Task ParseReport(byte[] bytesRead)
    {
        Stream stream = new MemoryStream(bytesRead);
        using (StreamReader reader = new StreamReader(stream))
            {
            string line = null;
            while (null != (line = reader.ReadLine())) 
            {
              string[] values = line.Split(';');
            }
            }
        stream.Close();
    }
于 2013-06-26T09:00:21.180 に答える