0

リモートのftpサイトからファイルをダウンロードしてローカルのcドライブに保存し、そのファイルを別のサーバーにアップロードするプログラムを作成しました。問題は、ファイルがダウンロードされたときに、ローカルCで作成されたテキストファイル内にデータがなく、その理由がわからないことです。これが私が使っているコードです

// Download File
    public void download(string remoteFile, string localFile)
    {

        try
        {
            // Create an FTP Request 
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(downhost + "/" + remoteFile);
            // Log in to the FTP Server with the User Name and Password Provided
            ftpRequest.Credentials = new NetworkCredential(downuser, downpass);
            // When in doubt, use these options
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            /* Set HTTP Proxy to Null to avoid The Requested FTP Command Is Not Supported When Using HTTP Proxy error */
            ftpRequest.Proxy = null;
            // Specify the Type of FTP Request
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            // Establish Return Communication with the FTP Server
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            // Get the FTP Server's Response Stream
            ftpStream = ftpResponse.GetResponseStream();
            // Open a File Stream to Write the Downloaded File
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            // Buffer for the Downloaded Data 
            byte[] byteBuffer = new byte[bufferSize];
            int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
            // Download the File by Writing the Buffered Data Until the Transfer is Complete

            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                }

            }

            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            // Resource Cleanup
            localFileStream.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;

        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

プログラムを構築するための基礎としてhttp://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Classにあるコードを使用し、他の人がどのように書いたかについてGoogle検索を行いました。 ftpダウンロードスクリプトが、データが書き込まれていない理由を理解することはできません。

どんな助けでも大歓迎です。

4

4 に答える 4

0

これは少し遅れていることに気付きましたが、その例を使用しようとして同じ問題が発生していました。ここから例を使用してFTPダウンロードを行うことができました:

http://www.techrepublic.com/blog/howdoi/how-do-i-use-c-to-upload-and-download-files-from-an-ftp-server/165

于 2013-02-08T01:32:36.033 に答える
0

同じ方法で問題があります。システムを機能させる方法を見つけましたが、次のようにftpstreamコードを別のストリームにコピーする必要がありました: (vb.net では、申し訳ありません)

ftpStream.copyTo(MySecondStream)
ftpStream.close()
'do whatever you want with the copy now
Return MySecondStream

おそらく、最初のストリームがどのように処理されるか、およびそれが開いたままになる時間に関係があります。ここに質問を投稿しました: FTP ストリームを別の変数にコピーして呼び出し元のメソッドに返す必要があるのはなぜですか?

于 2013-05-27T05:36:23.327 に答える
0

ファイルをフラッシュします。localFileStream.Flush();閉める前にお電話ください。

于 2012-12-05T17:04:22.113 に答える