ftpブレークポイントリレー

4665 ワード

ガイド:  ftp serverがREST命令をサポートしている場合(ftp clientで登録してREST 100を入力してサービス側の応答を確認してください)、アップロードまたはダウンロードに失敗した後、次の接続が成功したら断点からデータを転送し続けます。
 
        以下はDelphi 7に基づくコンポーネントIdFTPの一例コードで、自分がコマンドで実現するよりも便利です。
        
        ブレークポイントアップロード:
procedure TFtpThread.Execute;
var
  AFileStream: TFileStream;
  sFileName: string;
begin
  sFileName := ExtractFileName(AFileName);
  AFileStream := TFileStream.Create(AFileName, fmOpenRead);
  try
  with frmMain.IdFTP2 do
  try
    if Connected then
      Disconnect; //    
    Username := 'zzzz';
    Password := 'yyyyyy';
    Host := 'xx.xx.xx.xx';
    Port := 21;
    Passive := mypassive;
    Connect;
   except
    on e: Exception do
    begin
        frmMain.StatusBar1.Panels[0].Text := '  ,  :' + E.Message;
        frmMain.WriteLog(E.Message);
        AFileStream.Free;
        exit;
    end;
   end;
    //     
    finishedSize := frmMain.IdFTP2.Size(sFileName);
    AFileStream.Seek(finishedSize, soFromBeginning);
    //       
    BytesToTransfer := AFileStream.Size;
    frmMain.ProgressBar1.Max :=  BytesToTransfer div 1024;
    AFileStream.Position := finishedSize;
    
   try
    {while (not Terminated) and (AFileStream.Position < AFileStream.Size) do
    begin
      frmMain.IdFTP2.Put(AFileStream, sFileName, true);
    end;
    AMemStream.Free; }
     frmMain.IdFTP2.Put(AFileStream, sFileName, true);
   except
    on e: Exception do
    begin
        frmMain.StatusBar1.Panels[0].Text := '  ,  :' + E.Message;
        frmMain.WriteLog(E.Message);
        AFileStream.Free;
        exit;
    end;
   end;
  //         
  if AFileStream.Position = AFileStream.Size then
    frmMain.StatusBar1.Panels[0].Text := '    !'
  else
    frmMain.StatusBar1.Panels[0].Text := '     ,        !';
    AFileStream.Free;
  except
      on e: Exception do
    begin
        frmMain.StatusBar1.Panels[0].Text := '  ,  :' + E.Message;
        frmMain.WriteLog(E.Message);
        AFileStream.Free;
        exit;
    end;
  end;
end;
 
   断点ダウンロード:
  
procedure TfrmMain.FtpDownLoad(aURL, aFile: string);
var
  tStream: TFileStream;
  sName, sPass, sHost, sPort, sDir,sFile: string;
  bResume: Boolean;
begin //ftp    
  if FileExists(aFile) then //     
    tStream := TFileStream.Create(aFile, fmOpenWrite)
  else
    tStream := TFileStream.Create(aFile, fmCreate);

  GetFTPParams(aURL, sName, sPass, sHost, sPort, sDir,sFile);
  with IdFTP1 do
  try
    if Connected then
      Disconnect; //    
    Username := sName;
    Password := sPass;
    Host := sHost;
    Port := StrToInt(sPort);
    Passive := mypassive;
    Connect;
   except
   on e: Exception do
   begin
        Application.MessageBox(PChar(E.Message), 'error', 16);
        WriteLog(E.Message);
        exit;
   end;
   end;
  IdFTP1.ChangeDir(sDir); //    
  BytesToTransfer := IdFTP1.Size(sFile);
  ProgressBar1.Max := BytesToTransfer div 1024;
  if FileExists(aFile) then
    bResume := true
  else
     bResume := false;

  try
    if bResume then //  
    begin
      //ProgressBar1.Position := tStream.Size div 1024;
      finishedSize := tStream.Size;
      tStream.Position := tStream.Size;
      IdFTP1.Get(sFile, tStream, True);
    end
    else begin
      IdFTP1.Get(sFile, tStream, False);
    end;
    if tStream.Position = BytesToTransfer then
    begin
      StatusBar1.Panels[0].Text := '    ';
    end;
  finally
    tStream.Free;
  end;
end;

//ftp://user:[email protected]:2003/soft/list.mp4
procedure TfrmMain.GetFTPParams(var aURL, sName, sPass, sHost, sPort,
  sDir,sFile: String);
var
  URI: TIdURI;
begin
  URI := TIdURI.Create(aURL); //  
  try
    sHost := URI.Host; //  
    sPort := URI.Port;
    sName := URI.Username;
    sPass := URI.Password;
    sDir := URI.Path;
    sFile := URI.Document;
  finally
    URI.Free;
  end;
end;