libcurl使用ftp的一个例子

#include <stdio.h>

#include "curl.h"
//#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>
#include <unistd.h>
#include<string.h>

#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 \n",buf_1,buf_2);
	strcpy(URL,url);
	strcat(URL,"/");
	//strcat(URL,UPLOAD_FILE_AS);
	strcat(URL,filename);
	printf("PRRR url  %s\n",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 \n",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 \n%d %s \n",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...\n", 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\n",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\n pass the file name you are trying to transfer in the command line\n", argv[0]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值