返回> 网站首页
springboot接收curl上传的多个图片文件
yoours2023-02-06 19:28:36
简介一边听听音乐,一边写写文章。
一、curl上传文件
// POST
curl_easy_setopt(curl, CURLOPT_URL, url); //url地址
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ResponseData); //对返回的数据进行操作的函数地址
curl_easy_setopt(curl, CURLOPT_WRITEDATA, urlres); //这是write_data的第四个参数值
curl_easy_setopt(curl, CURLOPT_POST, 1); //设置问非0表示本次操作为post
curl_easy_setopt(curl, CURLOPT_HEADER, 0); //1 将响应头信息和相应体一起传给write_data
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //设置为非0, 响应头信息location
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); // 以下3个为重定向设置
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //返回的头部中有Location(一般直接请求的url没找到),则继续请求Location对应的数据
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3);//查找次数,防止查找太深
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 25);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
/*
// 提交多个图片方法一
struct curl_forms forms[6];
forms[0].option = CURLFORM_FILE;
forms[0].value = "d:\\1.jpg";
forms[1].option = CURLFORM_FILE;
forms[1].value = "d:\\2.jpg";
forms[2].option = CURLFORM_FILE;
forms[2].value = "d:\\3.jpg";
forms[3].option = CURLFORM_FILE;
forms[3].value = "d:\\4.jpg";
forms[4].option = CURLFORM_FILE;
forms[4].value = "d:\\5.jpg";
forms[5].option = CURLFORM_END;
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "pictures",
CURLFORM_ARRAY, forms,
CURLFORM_END);
*/
// 上传的文件
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME,"pic1",
CURLFORM_FILE, "d:\\1.jpg",
CURLFORM_CONTENTTYPE,"image/jpeg",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME,"pic2",
CURLFORM_FILE, "d:\\2.jpg",
CURLFORM_CONTENTTYPE,"image/jpeg",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME,"pic3",
CURLFORM_FILE, "d:\\3.jpg",
CURLFORM_CONTENTTYPE,"image/jpeg",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME,"pic4",
CURLFORM_FILE, "d:\\4.jpg",
CURLFORM_CONTENTTYPE,"image/jpeg",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME,"pic5",
CURLFORM_FILE, "d:\\5.jpg",
CURLFORM_CONTENTTYPE,"image/jpeg",
CURLFORM_END);
// 表单
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "name",
CURLFORM_COPYCONTENTS, "jack",
CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
// 增加自定义头
curl_slist *plist = curl_slist_append(NULL, "Content-Type:multipart/form-data");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist);
CURLcode res = curl_easy_perform(curl);
curl_formfree(formpost);
if (plist!=NULL)
curl_slist_free_all(plist);
curl_easy_cleanup(curl);