当前位置:网站首页>Use libcurl to upload the image of Opencv Mat to the file server, based on two methods of post request and ftp protocol
Use libcurl to upload the image of Opencv Mat to the file server, based on two methods of post request and ftp protocol
2022-08-02 15:27:00 【Hongyao】
基于post请求的
基于postThe method of request is mainly to upload to such as installednginx-upload-module的nginx服务器上,urlThe directory where the parameter passed file is located,底下的curl_formaddadd filename.
不过nginx-upload-modulewill not put the file filename,Instead, save it with the name 00000000xx这样的文件10digit-named files,Prevent upload file conflicts,需要你自己写php或pythonThe script moves the file to the correct location or renames it.
The design seems to make sense(Instead, provide a script for Blue Dog).
size_t writer(char *data, size_t size, size_t nmemb, string *writerData) {
if (writerData == nullptr)
return 0;
size_t len = size * nmemb;
writerData->append(data, len);
return len;
}
void postUpLoad(const cv::Mat &img, const char *url) {
vector<uchar> vec_Img;
vector<int> vecCompression_params;
vecCompression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
vecCompression_params.push_back(90);
imencode(".jpg", img, vec_Img, vecCompression_params);
CURL *curl;
CURLcode res;
string buffer;
struct curl_httppost *post = nullptr;
struct curl_httppost *last = nullptr;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1);
struct curl_slist *chunk = nullptr;
chunk = curl_slist_append(chunk, "Accept:");
chunk = curl_slist_append(chunk, "Authorization: Token ");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "photo",
CURLFORM_BUFFER, "photo.jpg",
CURLFORM_BUFFERPTR, vec_Img.data(),
CURLFORM_BUFFERLENGTH, vec_Img.size(),
CURLFORM_CONTENTTYPE, "image/jpg",
CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
// curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
//
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
}
基于ftp协议的
urlPass the file path directly,比如ftp://server.name/subdir/image.jpg
struct TmpStream {
void *ptr;
size_t last_num;
};
static size_t read_callback(void *ptr, size_t size, size_t nmemb, TmpStream *stream) {
if (stream->last_num <= 0)
return 0;
size_t retcode = size * nmemb;
retcode = MIN(retcode, stream->last_num);
memcpy(ptr, stream->ptr, retcode);
stream->last_num -= retcode;
// curl_off_t nread = (curl_off_t) retcode;
// fprintf(stderr, " We read %" CURL_FORMAT_CURL_OFF_T
// " bytes from file\n", nread);
return retcode;
}
void ftpUpload(const cv::Mat &img, const char *url) {
vector<uchar> vec_Img;
vector<int> vecCompression_params;
vecCompression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
vecCompression_params.push_back(90);
imencode(".jpg", img, vec_Img, vecCompression_params);
// struct curl_slist *headerlist = nullptr;
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_USERPWD, "smc:123456");
/* we want to use our own write function */
// curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/**create missing dir**/
curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* 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 */
TmpStream stream = {
vec_Img.data(), vec_Img.size()};
curl_easy_setopt(curl, CURLOPT_READDATA, &stream);
/* 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) vec_Img.size());
/* Now run off and do what you have been told! */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* clean up the FTP commands list */
// curl_slist_free_all(headerlist);
/* always cleanup */
curl_easy_cleanup(curl);
}
}
边栏推荐
- 【我的电赛日记(三)】STM32学习笔记与要点总结
- Makefile容易犯错的语法
- Win11没有本地用户和组怎么解决
- Win10 Settings screen out from lack of sleep?Win10 set the method that never sleep
- 将SSE指令转换为ARM NEON指令
- BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
- Win10无法连接打印机怎么办?不能使用打印机的解决方法
- 编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
- Use tencent cloud builds a personal blog
- DP1101兼容CC1101是SUB1GHz无线收发芯片应用于智能家居
猜你喜欢

Do Windows 10 computers need antivirus software installed?

Win10 computer can't read U disk?Don't recognize U disk how to solve?

Tensorflow常用函数

神经网络的设计过程

【STM32学习1】基础知识与概念明晰

win11一直弹出用户账户控制怎么解决

cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so

MATLAB绘制平面填充图入门详解

GICv3/v4-软件概述

Win10 cannot directly use photo viewer to open the picture
随机推荐
Mysql connection error solution
Binder机制(下篇)
基于51单片机和物联网的智能家居系统(ESP8266物联网模块)
如何用硬币模拟1/3的概率,以及任意概率?
BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
C语言函数参数传递模式入门详解
pytorch模型转libtorch和onnx格式的通用代码
Win10安装了固态硬盘还是有明显卡顿怎么办?
【使用Pytorch实现VGG16网络模型】
MATLAB图形加标注的基本方法入门简介
FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图
Mysql连接错误解决
Win10上帝模式干嘛的?Win10怎么开启上帝模式?
The SSE instructions into ARM NEON
Win11 keeps popping up User Account Control how to fix it
flink+sklearn——使用jpmml实现flink上的机器学习模型部署
2021-10-14
Failed to install using npx -p @storybook/cli sb init, build a dedicated storybook by hand
推开机电的大门《电路》(三):说说不一样的电阻与电导
DP4301无线收发SUB-1G芯片兼容CC1101智能家居