curl实现文件上传、下载

1. 文件下载

#include <string>
#include "../../opensource/curl/include/curl/curl.h"

using namespace std;

size_t write_data(void* ptr, size_t size, size_t nmemb, void* stream) {
    
    
	size_t written = fwrite(ptr, size, nmemb, (FILE*)stream);
	return written;
}

/**
 * 
 * \brief  DownloadFile
 * \param url: http 服务器地址
 * \param save_path:下载文件保存路径
 * \return 0-成功,1-失败
 */
int DownloadFile(std::string url, std::string save_path) {
    
    
	CURL* curl;
	CURLcode res;
	
	FILE *file = fopen(save_path.c_str(), "wb");
	if (!file) {
    
    
		std::cerr << "Failed to create file: " << save_path << std::endl;
		return 1;
	}

	curl = curl_easy_init();
	if (curl) {
    
    
		//设置curl的请求头
		struct curl_slist* header_list = NULL;
		header_list = curl_slist_append(header_list, "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);

		//不接收响应头数据0代表不接收 1代表接收
		curl_easy_setopt(curl, CURLOPT_HEADER, 0);

		//设置请求的URL地址 
		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

		//设置ssl验证
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

		//CURLOPT_VERBOSE的值为1时,会显示详细的调试信息
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);

		curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);

		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);

		curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);

		//设置超时时间
		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 6); // set transport and time out time  
		curl_easy_setopt(curl, CURLOPT_TIMEOUT, 6);

		res = curl_easy_perform(curl);
		if (res != CURLE_OK) {
    
    
			std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
			return 1;
		}
		curl_easy_cleanup(curl);
	}

	fclose(file);

	return 0;
}

2. 文件上传

#include <string>
#include "../../opensource/curl/include/curl/curl.h"

using namespace std;

typedef struct
{
    
    
	bool                     send_state;     //发送的状态
	std::string              data;       //接收到的数据
} CURL_CMD;

//curl 调用的回调函数
 size_t response_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
    
    
	if (!stream) return (size * nmemb);
	CURL_CMD * Curl_Ptr = (CURL_CMD *)stream;
	Curl_Ptr->data.assign((char*)ptr, nmemb*size);
	Curl_Ptr->send_state = true;

	return (size * nmemb);
}

int UploadFile(string url, string file_path, void *response)
{
    
    
	CURL* curl;
	CURLcode res = CURLE_OK;
	/* In windows, this will init the winsock stuff */
	curl_global_init(CURL_GLOBAL_ALL);
	struct curl_slist* list = NULL;
	///* get a curl handle */
	curl = curl_easy_init();
	if (curl) {
    
    
		/* First set the URL that is about to receive our POST. This URL can
		just as well be a https:// URL if that is what should receive the
		data. */
		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

		/* Now specify the POST data */
		struct curl_httppost* post = NULL;
		struct curl_httppost* last = NULL;

		int pos = file_path.find_last_of("/");
		string file_name = file_path.substr(pos + 1, file_path.length());

		// form-data key(file) "./test.jpg"为文件路径  "hello.jpg" 为文件上传时文件名
		curl_formadd(&post, &last, CURLFORM_PTRNAME, "file", CURLFORM_FILE, file_path.c_str(), CURLFORM_FILENAME, file_name.c_str(), CURLFORM_END);
		//构造post参数
		curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
		
		curl_easy_setopt(curl, CURLOPT_PORT, tcp_port);

		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, response_callback);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);                 

		/* Perform the request, res will get the return code */
		res = curl_easy_perform(curl);
		/* Check for errors */
		if (res != CURLE_OK)
		{
    
    
			fprintf(stderr, "curl_easy_perform() failed: %s cmd=%s\n",
				curl_easy_strerror(res), strip);
		}

		/* always cleanup */
		curl_easy_cleanup(curl);
		curl_slist_free_all(list); /* free the list again */
	}

	curl_global_cleanup();

	return res;
}

int main()
{
    
    
	CURL_CMD cmd;
	cmd.send_state = false;
	UploadFile("http://192.168.1.88:80/upload", "image/1.jpg", &cmd);
	printf("send state:%d\n", cmd.send_state);
	printf("recv:%s\n", cmd.data.c_str());
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wyw0000/article/details/131219967