2009-09-30 3 views
0

FTP를 통해 파일을 원격 컴퓨터에 보내려고합니다. 나는 전송/수신을위한 모든 코드를 성공적으로 작성했다. 이제 다른 IP (3 Ips)에 다른 파일 (예 : 3 파일)을 보내려고합니다. 그것을하는 방법?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

이미 코드를 작성했다면 공유하지 않으시겠습니까? –

+0

나는 그것을 게시 할 것이다. – karthik

답변

-3

나는 우리가 다른 구현 내가 업로드 여러 작업을 수행하는 스레드를 사용 할 수있는 작동했다 동시에 작동합니다.

관련 문제