2011-06-01 34 views
6

第一次張貼海報,長期閱讀。我有一個非常討厭的問題,這讓我感到很緊張。我已經安裝了一個程序,以便在FTP服務器上偵聽新文件,如果這是我下載的新文件。從那裏我開始處理文件中的一些信息等。當我第二次運行我的序列時,我的問題就出現了。也就是說,在我下載的第一個文件中,一切都很完美,但是一旦檢測到新文件並且我的程序嘗試下載它,我的程序就會掛起。程序掛在FtpWebResponse

private static void DownloadFile(string s) 
    { 
     try 
     { 
      FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://blabla.com/"+s); 
      request.Method = WebRequestMethods.Ftp.DownloadFile; 
      request.Credentials = new NetworkCredential("xxx" ,"zzz"); 

      using (FtpWebResponse partResponse = (FtpWebResponse)request.GetResponse()) 
      { 
       Stream partReader = partResponse.GetResponseStream(); 

       byte[] buffer = new byte[1024]; 
       FileInfo fi = new FileInfo(path); 
       FileStream memStream = fi.Create(); 
       while (true) 
       { 
        int bytesRead = partReader.Read(buffer, 0, buffer.Length - 1); 
        if (bytesRead == 0) 
         break; 

        memStream.Write(buffer, 0, bytesRead); 
       } 
       partResponse.Close(); 
       memStream.Close(); 
      } 
      Console.WriteLine(DateTime.Now + " file downloaded"); 
      MoveFileToInProgress(s); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
     } 
    } 

它掛在該生產線是這一個: 使用(FtpWebResponse partResponse =(FtpWebResponse)request.GetResponse())

的原因就在這裏我的方法是靜態的,是因爲我只是運行它在不同的項目來測試它..我的問題是,它怎麼只死在第二個文件?我一直盯着自己幾個小時了!

回答

0

我不是C#專家,但我使用此代碼下載從我的FTP文件:

public void Download(string filename) 
    { 
     // I try to download five times before crash 
     for (int i = 1; i < 5; i++) 
     { 
      try 
      { 
       FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(Global.Path + "/" + filename); 
       ftp.Credentials = new NetworkCredential(User, Pass); 
       ftp.KeepAlive = false; 
       ftp.Method = WebRequestMethods.Ftp.DownloadFile; 
       ftp.UseBinary = true; 
       ftp.Proxy = null; 

       int buffLength = 2048; 
       byte[] buff = new byte[buffLength]; 
       int contentLen; 

       string LocalDirectory = Application.StartupPath.ToString() + "/downloads/" + filename; 
       using (FileStream fs = new FileStream(LocalDirectory, FileMode.Create, FileAccess.Write, FileShare.None)) 
       using (Stream strm = ftp.GetResponse().GetResponseStream()) 
       { 
        contentLen = strm.Read(buff, 0, buffLength); 
        while (contentLen != 0) 
        { 
         fs.Write(buff, 0, contentLen); 
         contentLen = strm.Read(buff, 0, buffLength); 
        } 
       } 

       Process.Start(LocalDirectory); 
       break; 
      } 
      catch (Exception exc) 
      { 
       if (i == 5) 
       { 
        MessageBox.Show("Can't download, try number: " + i + "/5 \n\n Error: " + exc.Message, 
         "Problem downloading the file"); 
       } 
      } 
     } 
    } 

告訴我,如果你的作品:)

+0

我程序仍然掛在同一行上,在你的代碼中它轉換爲這一行:using(Stream strm = ftp.GetResponse()。GetResponseStream())我想我將不得不去尋找我沒有正確關閉FTP連接的地方,東西.. – MartinNielsen 2011-06-03 06:15:04

+1

你可以嘗試讓計時器在幾秒鐘內斷開和/或重試(如果procces需要更長的時間,則更長) – Daanvl 2011-09-02 14:41:50

+0

你有沒有想過這個?我的代碼也掛在最後幾個字節 – 2013-04-03 12:02:41

2

我就遇到了這個問題,以及...先嚐試完成您的請求,然後在嘗試檢索響應之前關閉它。這對我很有用(實際上是在閱讀MartinNielsen的評論後嘗試的)。這是我做的。

 // connect to the ftp site 
     FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpUri); 
     ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; 
     ftpRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword); 

     // setting proxy to null so that it does not go through the proxy 
     ftpRequest.Proxy = null; 

     // get file information 
     StreamReader fileStream = new StreamReader(filePath); 
     byte[] fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd()); 
     ftpRequest.ContentLength = fileBytes.Length; 
     fileStream.Close(); 

     // open connection to ftp site 
     Stream ftpRequestStream = ftpRequest.GetRequestStream(); 

     // write the file to the stream 
     ftpRequestStream.Write(fileBytes, 0, fileBytes.Length); 

     // close the stream 
     ftpRequestStream.Close(); 

     // get the response from the server 
     FtpWebResponse ftpUploadResponse = (FtpWebResponse)ftpRequest.GetResponse(); 
     string result = ftpUploadResponse.StatusDescription; 

     // close response 
     ftpUploadResponse.Close(); 

     // return response to calling code 
     return result; 

這裏有一對夫婦,我寫這個代碼時使用的資源(不會讓我上傳超過2個,還有更多)

How to: Upload Files with FTP

Uploading a file -- "The requested URI is invalid for this FTP command"

+0

我不明白爲什麼這個答案是upvoted?!問題是關於下載*不上傳文件。它不使用任何需要關閉的請求流。那麼這怎麼可能被認爲是這個問題的答案呢? – 2017-01-25 17:41:02