Ir al contenido


Foto

[RESUELTO] Curl puede transferir archivos a un sitio sFTP ?


  • Por favor identifícate para responder
21 respuestas en este tema

#21 seoane

seoane

    Advanced Member

  • Administrador
  • 1.259 mensajes
  • LocationEspaña

Escrito 25 octubre 2019 - 06:26

El problema que tienes es que no estas creando el stream antes de usarlo.

 

De todas formas aquí te va un ejemplo de como bajar todos los ficheros de un directorio conociendo su extension:

 


delphi
  1. program EjemploSftp;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   Windows,
  7.   Sysutils,
  8.   Classes,
  9.   Curl in 'Curl.pas';
  10.  
  11.  
  12. function downloadFile(url, user, password: AnsiString; localPath: String): CURLCode;
  13. var
  14.   hCurl: TCurl;
  15.   fileStream: TFileStream;
  16. begin
  17.   Result:= CURLE_OK;
  18.   hCurl:= curl_easy_init;
  19.   if hCurl <> nil then
  20.   try
  21.     if curl_easy_setopt(hCurl, CURLOPT_VERBOSE, TRUE) <> CURLE_OK then Exit;
  22.     if curl_easy_setopt(hCurl, CURLOPT_USE_SSL, CURLUSESSL_ALL) <> CURLE_OK then Exit;
  23.     if curl_easy_setopt(hCurl, CURLOPT_SSL_VERIFYPEER, FALSE) <> CURLE_OK then Exit;
  24.     if curl_easy_setopt(hCurl, CURLOPT_USERNAME, PAnsiChar(user)) <> CURLE_OK then Exit;
  25.     if curl_easy_setopt(hCurl, CURLOPT_PASSWORD, PAnsiChar(password)) <> CURLE_OK then Exit;
  26.     if curl_easy_setopt(hCurl, CURLOPT_URL, PAnsiChar(url)) <> CURLE_OK then Exit;
  27.     if curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, @CurlSaveToStream) <> CURLE_OK then Exit;
  28.     fileStream:= TFileStream.Create(localPath, fmCreate);
  29.     try
  30.       if curl_easy_setopt(hCurl, CURLOPT_FILE, fileStream) <> CURLE_OK then Exit;
  31.       Result:= curl_easy_perform(hCurl);
  32.     finally
  33.       fileStream.Free;
  34.     end;
  35.   finally
  36.     curl_easy_cleanup(hCurl);
  37.   end;
  38. end;
  39.  
  40. function downloadDir(url, user, password, fileExt: AnsiString; localPath: String): CURLCode;
  41. var
  42.   i: Integer;
  43.   hCurl: TCurl;
  44.   list: TMemoryStream;
  45.   name: String;
  46. begin
  47.   Result:= CURLE_OK;
  48.   if Copy(url, High(url), 1) <> '/' then
  49.     url:= url + '/';
  50.   hCurl:= curl_easy_init;
  51.   if hCurl <> nil then
  52.   try
  53.     if curl_easy_setopt(hCurl, CURLOPT_VERBOSE, TRUE) <> CURLE_OK then Exit;
  54.     if curl_easy_setopt(hCurl, CURLOPT_USE_SSL, CURLUSESSL_ALL) <> CURLE_OK then Exit;
  55.     if curl_easy_setopt(hCurl, CURLOPT_SSL_VERIFYPEER, FALSE) <> CURLE_OK then Exit;
  56.     if curl_easy_setopt(hCurl, CURLOPT_USERNAME, PAnsiChar(user)) <> CURLE_OK then Exit;
  57.     if curl_easy_setopt(hCurl, CURLOPT_PASSWORD, PAnsiChar(password)) <> CURLE_OK then Exit;
  58.     if curl_easy_setopt(hCurl, CURLOPT_URL, PAnsiChar(url)) <> CURLE_OK then Exit;
  59.     if curl_easy_setopt(hCurl, CURLOPT_DIRLISTONLY, TRUE) <> CURLE_OK then Exit;
  60.     if curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, @CurlSaveToStream) <> CURLE_OK then Exit;
  61.     list:= TMemoryStream.Create;
  62.     try
  63.       if curl_easy_setopt(hCurl, CURLOPT_FILE, list) <> CURLE_OK then Exit;
  64.       Result:= curl_easy_perform(hCurl);
  65.       with TStringList.Create do
  66.       try
  67.         list.Position:= 0;
  68.         LoadFromStream(list);
  69.  
  70.         localPath:= IncludeTrailingPathDelimiter(ExpandFileName(localPath));
  71.         if not ForceDirectories(localPath) then Exit;
  72.         for i:= 0 to Count - 1 do
  73.         begin
  74.           name:= Strings[i];
  75.           if (name <> '.') and (name <> '..') then
  76.             if ExtractFileExt(name) = fileExt then
  77.               downloadFile(url + name, user, password, localPath + name);
  78.         end;
  79.       finally
  80.         Free;
  81.       end;
  82.     finally
  83.       list.Free;
  84.     end;
  85.   finally
  86.     curl_easy_cleanup(hCurl);
  87.   end;
  88. end;
  89.  
  90. begin
  91.   downloadDir('sftp://test.rebex.net:22', 'demo', 'password', '.txt', 'c:\abc');
  92. end.

 

Esta hecho de forma rápida, funciona, pero seguro que tendrás que pulir algunas cosas

 

Saludos


  • 0

#22 Ana

Ana

    Newbie

  • Miembros
  • Pip
  • 4 mensajes

Escrito 30 octubre 2019 - 02:50

Hola

No lo he podido probar todavía, porque ahora tengo otro problema. Como os comenté podía descargar un fichero y para mi sorpresa ahora me reporta el siguiente error  CURLE_COULDNT_RESOLVE_HOST. Vamos que ha dejado de funcionar de un día para otro. Voy a seguir peleando y cuando tenga esto resuelto pruebo la descarga y os comento.

Muchas gracias.


  • 1




IP.Board spam blocked by CleanTalk.