libcurl使用ftp的一个例子

2019-07-12 18:06发布

#include #include "curl.h" //#include #include #include #include #include #include #define REMOTE_URL "ftp://192.168.1.164" int Upload_FTPfile(char *url,char * filename) { CURL *curl; CURLcode res; FILE * hd_src ; char buff[100]=""; int hd ; struct stat file_info; struct curl_slist *headerlist=NULL; //static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS; char buf_1 [20] = "RNFR "; char buf_2 [20] = "RNTO "; char URL[1024]=""; strcat(buf_1,filename);//// strcat(buf_2,filename); printf("bufs %s %s ",buf_1,buf_2); strcpy(URL,url); strcat(URL,"/"); //strcat(URL,UPLOAD_FILE_AS); strcat(URL,filename); printf("PRRR url %s ",URL); /* get the file size of the local file */ hd = open(filename, O_RDONLY) ; fstat(hd, &file_info); close(hd) ; /* get a FILE * of the same file, could also be made with fdopen() from the previous descriptor, but hey this is just an example! */ hd_src = fopen(filename,"rb"); /* In windows, this will init the winsock stuff */ curl_global_init(CURL_GLOBAL_ALL); /* get a curl handle */ curl = curl_easy_init(); //This code do the first step. if(curl) { /* build a list of commands to pass to libcurl */ headerlist = curl_slist_append(headerlist, buf_1); headerlist = curl_slist_append(headerlist, buf_2); //headerlist = curl_slist_append(headerlist, "STOR"); /* enable uploading */ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1) ; //curl_easy_setopt(curl,CURLOPT_FTPAPPEND, 1<<20) ; curl_easy_setopt(curl, CURLOPT_ERRORBUFFER ,buff); // curl_easy_setopt(curl,CURLOPT_USERPWD,"pdg:pdg123"); //printf("URL %s ",REMOTE_URL); /* specify target */ curl_easy_setopt(curl,CURLOPT_URL, URL); /* pass in that last of FTP commands to run after the transfer */ curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); /* now specify which file to upload */ curl_easy_setopt(curl, CURLOPT_READDATA, hd_src); /* NOTE: if you want this example to work on Windows with libcurl as a DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to do so will give you a crash since a DLL may not use the variable's memory when passed in to it from an app like this. */ /* Set the size of the file to upload (optional). If you give a *_LARGE option you MUST make sure that the type of the passed-in argument is a curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must make sure that to pass in a type 'long' argument. */ curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size); /* Now run off and do what you've been told! */ res = curl_easy_perform(curl); printf("UP %d %s ",res,buff); /* clean up the FTP commands list */ curl_slist_free_all (headerlist); /* always cleanup */ curl_easy_cleanup(curl); fclose(hd_src); /* close the local file */ } curl_global_cleanup(); return 0; } int GET_file_clear(char *http_url, char *filename) { CURL *curl; CURLcode res; FILE *out_fd = (FILE *) 0; int result = 0; char buff[100]=""; curl = curl_easy_init(); if(curl) { printf("Downloading %s file... ", filename); out_fd = fopen (filename, "w");//open for read and write curl_easy_setopt(curl, CURLOPT_FILE, out_fd); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER ,buff); //curl_easy_setopt(curl, CURLOPT_HEADER, 0); curl_easy_setopt(curl, CURLOPT_URL, http_url); //curl_easy_setopt(curl,CURLOPT_USERPWD,"Pilot:Pilot123"); // curl_easy_setopt(curl,CURLOPT_USERPWD,"pdg:pdg123"); //Pilot is the User name //Pilot123 is the Password for the server res = curl_easy_perform(curl); //post away! if(res == 0) result = 1; else //message was not successfully posted to server result = 0; fclose(out_fd); } printf("%d %s ",result,buff); //there must be a corresponding curl_easy_cleanup() to curl_easy_init() curl_easy_cleanup(curl); return result; } /*----------------- end of function -------------------*/ //int upload(char *ptr) /*int main() { char *ptr ="abc.txt"; Upload_FTPfile(REMOTE_URL,ptr); //Upload_FTPfile("ftp://202.153.41.201/Test/","Demo1.txt"); //GET_file_clear(REMOTE_URL ,"ndpl.db"); // GET_file_clear("ftp://202.153.41.201/Test/Demo1.txt" ,"ftp.txt123"); }*/ int main(int argc, char *argv[]) { //char *ptr ="abc.txt"; if(argc >= 2) { Upload_FTPfile(REMOTE_URL,argv[1]); GET_file_clear(REMOTE_URL ,argv[2]); } else printf("usage of %s is wrong pass the file name you are trying to transfer in the command line ", argv[0]); }