前言
最近的项目中要把生成的图片和视频上传的云平台。
一、libcurl开源库
libcurl作为是一个多协议的便于客户端使用的URL传输库,基于C语言,提供C语言的API接口,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP这些协议,同时支持使用SSL证书的安全文件传输:HTTP POST, HTTP PUT, FTP 上传, 基于HTTP形式的上传、代理、Cookies、用户加密码的认证等多种应用场景。
二、上传图像和视频的代码
int uploadfile(char *url, char *file_path, char * cookie_path, char ** outdata,int file_type)
{
CURL *curl;
CURLcode rcode;
char *filedata = NULL;
int fileLen;
int ret;
CURLcode r = CURLE_GOT_NOTHING;
fileLen = file_size(file_path, &filedata);
printf("fileLen = %d \n", fileLen);
if (fileLen <= 0) {
if (filedata) {
free(filedata);
}
return -1;
}
curl = curl_easy_init();
if (NULL == curl)
return 0;
//设置url
curl_easy_setopt(curl, CURLOPT_URL, url);
//设置超时时间
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
//设置http以post方式请求
curl_easy_setopt(curl, CURLOPT_POST, 1L); //POST
struct curl_slist *headers=NULL;
if (file_type == FIlE_IMAGE) {
headers = curl_slist_append(headers, "Content-Type: image/jpeg");
}
else if (file_type == FILE_VIDEO) {
headers = curl_slist_append(headers, "Content-Type: video/mpeg4");
}
headers = curl_slist_append(headers, "Expect:");
//set headers
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
struct MemoryStruct chunk;
chunk.memory = (char *)malloc(sizeof(char)); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, PostCallBackData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //开启/关闭 打印调试详细信息模式
if (cookie_path) {
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_path);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_path);
}
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,filedata);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, fileLen);
//发送数据
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
r = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
if (r != CURLE_OK) {
if (chunk.memory)
free(chunk.memory);
printf("curl post CURLOPT_SSL_VERIFYPEER failed :%s\n",curl_easy_strerror(r));
goto EXIT;
}
rcode = curl_easy_perform(curl);
if (rcode != CURLE_OK)
{
*outdata = NULL;
if (chunk.memory)
free(chunk.memory);
printf("curl_easy_perform() failed,error code is:%s\n", curl_easy_strerror(rcode));
} else {
*outdata = chunk.memory;
}
EXIT:
curl_easy_cleanup(curl);
if (filedata) {
free(filedata);
}
if (headers) {
free(headers);
}
return 1;
}
static size_t PostCallBackData(void *buffer, size_t size, size_t nmemb, void *user_p)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)user_p;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), buffer, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
一开始主要是没有设置Content-Type,对http协议不是很了解,以为只要把文件的路径和格式传进去就ok了
https://www.runoob.com/http/http-content-type.html
这里有Content-Type对照表的格式,大家想上传别的格式的内容,只要更改Content-Type的内容就可以了。

本文介绍如何使用libcurl库进行图片和视频的上传,包括设置Content-Type、使用SSL证书的安全传输等,并提供了详细的代码示例。
847

被折叠的 条评论
为什么被折叠?



