在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            博客專欄

            EEPW首頁 > 博客 > FTP文件上傳、下載(基于curl庫 )

            FTP文件上傳、下載(基于curl庫 )

            發(fā)布人:電子禪石 時(shí)間:2022-12-27 來源:工程師 發(fā)布文章

            在Linux中curl是一個(gè)利用URL規(guī)則在命令行下工作的文件傳輸工具,如下為通過curl命令上傳、下載ftp文件的用法:

            # curl -T upload_test.txt -u fangye:fangye ftp://192.168.10.105/   //下載ftp文件 -u 賬號:密碼

            更多curl命令用法參考博客: https://blog.csdn.net/gubenpeiyuan/article/details/50803095


            libcurl庫為通信協(xié)議庫,支持HTTP, HTTPS, FTP, FTPS, GOPHER, TFTP, SCP, SFTP, SMB, TELNET, DICT, LDAP, LDAPS, FILE, IMAP, SMTP, POP3, RTSP 和 RTMP等多種協(xié)議,如下為基于curl庫中ftp協(xié)議實(shí)現(xiàn)的文件上傳、下載示例:


            ftp-manager.c

            ————————————————

            #include <stdio.h>
            #include <stdlib.h>
            #include <curl/curl.h>
             
            #include "ftp-manager.h"
             
            int get_file_size(FILE *file) //獲取文件大小
            {
            	int size = 0;
            	fseek(file, 0L, SEEK_END);
            	size = ftell(file);
            	fseek(file, 0L, SEEK_SET);
            	return size;
            }
             
            CURL *curl_init()          //curl初始化
            {
            	curl_global_init(CURL_GLOBAL_DEFAULT); 
            	CURL *curl = curl_easy_init();
            	if(NULL == curl)
            	{
            		fprintf(stderr, "Init curl failed.\n");
            		exit(1);
            	}
            	return curl;
            }
             
            void curl_set_upload_opt(CURL *curl, const char *url, const char *user_key, FILE *file) //設(shè)置上傳配置參數(shù)
            {
            	curl_easy_setopt(curl, CURLOPT_URL, url);
            	curl_easy_setopt(curl, CURLOPT_USERPWD, user_key);
            	curl_easy_setopt(curl, CURLOPT_READDATA, file);	
            	curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
            	curl_easy_setopt(curl, CURLOPT_INFILESIZE, get_file_size(file));
            	curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1);
            //	curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
            }
             
            void curl_set_download_opt(CURL *curl, const char *url, const char *user_key, FILE *file) //設(shè)置下載配置參數(shù)
            {
            	curl_easy_setopt(curl, CURLOPT_URL, url);
            	curl_easy_setopt(curl, CURLOPT_USERPWD, user_key);
            	curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
            //	curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
            }
             
            void curl_exit(CURL *curl)  //退出curl
            {
            	curl_easy_cleanup(curl);
            	curl_global_cleanup(); 
            }
             
            CURLcode curl_perform(CURL *curl) //執(zhí)行curl
            {
            	CURLcode ret = curl_easy_perform(curl);
            	if(ret != 0)
            	{
            		fprintf(stderr, "Perform curl failed.\n");
            		curl_exit(curl);
            		exit(1);
            	}
            	return ret;
            }
             
             
            FTP_STATE ftp_upload(const FTP_OPT ftp_option) //ftp上傳文件
            {
            	FTP_STATE state;
            	CURL *curl;;
            	FILE *fp = fopen(ftp_option.file, "r");
            	if(NULL == fp)
            	{
            		fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__);
            		return FTP_UPLOAD_FAILED;
            	}
             
            	curl = curl_init();
            	curl_set_upload_opt(curl, ftp_option.url, ftp_option.user_key, fp);
            	if(CURLE_OK == curl_perform(curl))
            		state = FTP_UPLOAD_SUCCESS;
            	else
            		state = FTP_UPLOAD_FAILED;
             
            	curl_exit(curl);
            	fclose(fp);
            	return state;
            }
             
            FTP_STATE ftp_download(const FTP_OPT ftp_option) // ftp下載文件
            {
            	FTP_STATE state;
            	CURL *curl;
            	FILE *fp = fopen(ftp_option.file, "w");
            	if(NULL == fp)
            	{
            		fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__);
            		return FTP_UPLOAD_FAILED;
            	}
             
            	curl = curl_init();
            	curl_set_download_opt(curl, ftp_option.url, ftp_option.user_key, fp);
            	if(CURLE_OK == curl_perform(curl))
            		state = FTP_DOWNLOAD_SUCCESS;
            	else
            		state = FTP_DOWNLOAD_FAILED;
             
            	curl_exit(curl);
            	fclose(fp);
            	return state;
            }

            ftp-manager.h

            #ifndef _FTP_MANAGER_H_
            #define _FTP_MANAGER_H_
             
            typedef enum FTP_STATE
            {
            	FTP_UPLOAD_SUCCESS,
            	FTP_UPLOAD_FAILED,
            	FTP_DOWNLOAD_SUCCESS,
            	FTP_DOWNLOAD_FAILED 
            }FTP_STATE;
             
            typedef struct FTP_OPT
            {
            	char *url;		/*url of ftp*/
            	char *user_key;		/*username:password*/
            	char *file;		/*filepath*/
            }FTP_OPT;
             
            #ifdef __cplusplus
            	extern "C" {
            #endif
             
             
            FTP_STATE ftp_upload(const FTP_OPT ftp_option); //上傳文件
             
             
            FTP_STATE ftp_download(const FTP_OPT ftp_option); //下載文件
             
            #ifdef __cplusplus
            	}
            #endif
             
            #endif

            main.c  //ftp文件上傳、下載調(diào)用curl示例

            #include <stdio.h>
            #include "ftp-manager.h"
             
            #define UPLOAD
            #define DOWNLOAD
             
            int main()
            {
            	FTP_OPT ftp_opt;
            	
            #ifdef UPLOAD
            	ftp_opt.url = "ftp://192.168.10.105/upload/upload_test.txt"; //傳到ftp服務(wù)器路徑名
            	ftp_opt.user_key = "fangye:fangye"; //ftp 賬號密碼   格式<賬號:密碼>  
            	ftp_opt.file = "./upload_test.txt"; //指本地文件要上傳的文件
            	if(FTP_UPLOAD_SUCCESS == ftp_upload(ftp_opt))
            		printf("Upload success.\n");
            	else
            		printf("Upload failed.\n");
            #endif
             
            #ifdef DOWNLOAD
            	ftp_opt.url = "ftp://192.168.10.105/download/ZeroMQ.pdf"; //ftp服務(wù)器文件
            	ftp_opt.file = "zmq.pdf";	//指下載到本地文件名
            	if(FTP_DOWNLOAD_SUCCESS == ftp_download(ftp_opt))
            		printf("Download success.\n");
            	else
            		printf("Download failed.\n");
            #endif
             
            	return 0;
            }


            *博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請聯(lián)系工作人員刪除。



            關(guān)鍵詞: curl

            技術(shù)專區(qū)

            關(guān)閉