2009-09-30 66 views
0

我試圖通過FTP經常發送文件到遠程機器。我已成功編寫了發送/接收的所有代碼。現在我想發送不同的文件(例如:3個文件)到不同的IP(3個Ips)。怎麼做?上傳/下載多個文件同時使用FTPWebrequest類

public bool UploadFile(string file_to_upload,int attempts) 
    { 


      if (System.IO.File.Exists(file_to_upload)) 
      { 
        FtpWebResponse response = null; 
        FtpWebRequest ftprequest= null;   
        int Appendinglength = 0; 
        int TotalLength = 0; 
        pointer = 1; 
        long FTPFilesize = 0; 
        while (--attempts >= 0) 
        { 
         try 
         { 
          FileInfo finfo = new FileInfo(file_to_upload); 
          TotalLength = Convert.ToInt32(finfo.Length); 

          ftprequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + IP + "/" + finfo.Name)); 
          ftprequest.Credentials = new NetworkCredential(Uname, Pwd); 
          ftprequest.UseBinary = true; 
          ftprequest.Timeout = 500; 
          ftprequest.KeepAlive = false; 
          if (ISFtpFileExists(finfo.Name, out FTPFilesize)) 
          { 
           ftprequest.Method = WebRequestMethods.Ftp.UploadFile; 
          } 
          else 
          { 
           ftprequest.Method = WebRequestMethods.Ftp.UploadFile; 
          } 

          //ftprequest.ContentLength = finfo.Length - FTPFilesize; 
          ftprequest.UsePassive = false; 
          fstr = new FileStream(file_to_upload, FileMode.Open); 
          ToWriteStream = ftprequest.GetRequestStream(); 
          //response = (FtpWebResponse)ftprequest.GetResponse(); 
          attempts = 3000; 
          //fstr.Seek(FTPFilesize, SeekOrigin.Begin); 
          byte[] buffer = new byte[bufferLength]; 
          fstr.Read(buffer, 0, bufferLength); 
          startuptime = DateTime.Now; 
          Exoccur("Resuming Upload.."); 
          while (pointer != 0) 
          { 
           ToWriteStream.Write(buffer, 0, pointer); 
           Appendinglength += pointer; 
           int prg = CalculateProgress(Appendinglength, TotalLength); 
           PrgUpdate(prg); 
           //lblupdate(prg); 
           pointer = fstr.Read(buffer, 0, bufferLength); 
          } 
          ToWriteStream.Close(); 
          fstr.Close(); 
          break; 
         } 
         catch 
         { 
          //ToWriteStream.Close(); 
          fstr.Close(); 
          if (attempts == 2999) 
          { 
           PrgUpdate(0); 
          } 
          Appendinglength = 0; 
          ftprequest.Abort(); 
          ftprequest = null; 
          Thread.Sleep(500); 

         } 
         finally 
         { 
          //attempts = 3000; 
         } 
        } 
        enduptime = DateTime.Now; 
        TimeSpan timetaken = enduptime.Subtract(startuptime); 
        double d = timetaken.TotalMilliseconds; 
        d = d/1000; 
        Exoccur(Convert.ToString(d)); 
      } 
      else 
      { 
       Exoccur("File Doesnt occur"); 

      } 
      return true; 
    } 

上面一個是上傳在FTP

public bool DownloadFile(string DestPath, string file_to_download) 
    { 
     PrgUpdate(0); 
     FtpWebRequest ftprequest = null; 
     int TotalLength = 0; 
     WebResponse response = null; 
     FileInfo fin = null; 
     try 
     { 
      int Appendinglength = 0; 
      if (KeepLength != 0) 
      { 
       TotalLength = KeepLength; 
      } 
      else 
      { 
       //FileStream fst = new FileStream(DestPath, FileMode.Create); 
       TotalLength = Convert.ToInt32(GetFileSize_Remotemachine(file_to_download)); 
      } 
      fin = new FileInfo(DestPath); 
      //FtpWebRequest ftprequest; 
      ftprequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + IP + "/" + file_to_download)); 
      ftprequest.Credentials = new NetworkCredential(Uname, Pwd); 
      ftprequest.Method = WebRequestMethods.Ftp.DownloadFile; 
      ftprequest.UseBinary = true; 
      ftprequest.KeepAlive = false; 
      if (fin.Exists) 
      { 
       ftprequest.ContentOffset = fin.Length; 
       fst = new FileStream(DestPath, FileMode.Append); 
      } 
      else 
      { 
       fst = new FileStream(DestPath, FileMode.Create); 
      } 
      response = (WebResponse)ftprequest.GetResponse(); 
      Stream GetStream = response.GetResponseStream(); 
      byte[] buffer = new byte[bufferLength]; 
      pointer = GetStream.Read(buffer, 0, bufferLength); 
      DateTime starttime = DateTime.Now; 
      while (pointer != 0) 
      { 
       fst.Write(buffer, 0, pointer); 
       Appendinglength += pointer; 
       PrgUpdate(CalculateProgress(Appendinglength, TotalLength)); 
       pointer = GetStream.Read(buffer, 0, bufferLength); 
      } 
      fin = new FileInfo(DestPath); 
      if ((long)TotalLength == fin.Length) 
      { 
       Exoccur("File download completed."); 
      } 
      else 
      { 
       PrgUpdate(100); 
      } 
      DateTime endtime = DateTime.Now; 
      TimeSpan diff = endtime.Subtract(starttime); 
      GetStream.Close(); 
      fst.Close(); 
      response.Close(); 
      double d = diff.TotalMilliseconds; 
      d = d/1000; 
      KeepLength = 0; 
      Exoccur(Convert.ToString(d)); 
     } 
     catch (Exception ex) 
     { 
      ftprequest.Abort(); 
      KeepLength = TotalLength; 
      PrgUpdate(0); 
      Exoccur(ex.Message); 
      return false; 
     } 
     finally 
     { 
      //response.Close(); 
      fst.Close(); 
     } 
     return true; 
    } 

而這一次是下載

+1

如果你已經寫了一些代碼,你爲什麼不分享它? – 2009-09-30 12:59:12

+0

當然我會發布它 – karthik 2009-09-30 13:46:22

回答

-3

我做到了工作,我們可以做不同的實現方式,我用線程做多 上傳作品