fix file upload
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
#include "interface.h"
|
||||
#include "rocketmq.h"
|
||||
#include "nlohmann/json.hpp"
|
||||
#include "log4cplus/log4.h"
|
||||
#include "log4.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -200,9 +200,28 @@ void handleUploadResponse(const std::string& response, std::string& wavepath) {
|
||||
|
||||
//上传文件
|
||||
void SendFileWeb(const std::string& strUrl, const std::string& localpath, const std::string& cloudpath, std::string& wavepath) {
|
||||
|
||||
// 基本存在性检查
|
||||
if (access(localpath.c_str(), F_OK) != 0) {
|
||||
std::cerr << "Local file does not exist: " << localpath << std::endl;
|
||||
return;
|
||||
}
|
||||
// ★新增:stat 打印大小,便于快速确认读源
|
||||
struct stat st {};
|
||||
if (stat(localpath.c_str(), &st) != 0) {
|
||||
perror("stat");
|
||||
} else {
|
||||
std::cout << "[debug] upload file: " << localpath
|
||||
<< ", size=" << static_cast<long long>(st.st_size) << " bytes\n";
|
||||
}
|
||||
|
||||
// 初始化 curl
|
||||
CURL* curl = curl_easy_init();
|
||||
if (curl) {
|
||||
// ★新增:错误缓冲,便于看到更具体的错误
|
||||
char errbuf[CURL_ERROR_SIZE] = {0};
|
||||
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); // ★新增
|
||||
|
||||
// 设置请求 URL 和 POST 请求
|
||||
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
||||
@@ -211,21 +230,16 @@ void SendFileWeb(const std::string& strUrl, const std::string& localpath, const
|
||||
curl_httppost* formpost = nullptr;
|
||||
curl_httppost* lastptr = nullptr;
|
||||
|
||||
if (access(localpath.c_str(), F_OK) != 0) {
|
||||
std::cerr << "Local file does not exist: " << localpath << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// 添加文件字段,直接从本地路径读取文件内容
|
||||
curl_formadd(&formpost, &lastptr,
|
||||
CURLFORM_COPYNAME, "file",
|
||||
CURLFORM_FILE, localpath,
|
||||
CURLFORM_FILE, localpath.c_str(),
|
||||
CURLFORM_END);
|
||||
|
||||
// 添加 `path` 字段
|
||||
curl_formadd(&formpost, &lastptr,
|
||||
CURLFORM_COPYNAME, "path",
|
||||
CURLFORM_COPYCONTENTS, cloudpath,
|
||||
CURLFORM_COPYCONTENTS, cloudpath.c_str(),
|
||||
CURLFORM_END);
|
||||
|
||||
// 添加 `isReserveName` 字段
|
||||
@@ -238,13 +252,14 @@ void SendFileWeb(const std::string& strUrl, const std::string& localpath, const
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
||||
|
||||
// 设置头信息
|
||||
struct curl_slist* header_list = nullptr;
|
||||
header_list = curl_slist_append(header_list, "Content-Type: multipart/form-data");
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
|
||||
//struct curl_slist* header_list = nullptr;
|
||||
//header_list = curl_slist_append(header_list, "Content-Type: multipart/form-data");
|
||||
//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
|
||||
|
||||
// 设置超时时间
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
|
||||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); // ★新增:避免多线程环境下超时触发信号
|
||||
|
||||
// 设置写入响应数据的函数
|
||||
std::string resPost0;
|
||||
@@ -254,7 +269,8 @@ void SendFileWeb(const std::string& strUrl, const std::string& localpath, const
|
||||
// 执行请求
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK) {
|
||||
std::cerr << "http web failed: " << curl_easy_strerror(res) << std::endl;
|
||||
const char* em = errbuf[0] ? errbuf : curl_easy_strerror(res);
|
||||
std::cerr << "http web failed: " << em << std::endl;
|
||||
DIY_ERRORLOG("process","【ERROR】前置上传暂态录波文件 %s 失败,请检查文件上传接口配置",localpath);
|
||||
} else {
|
||||
std::cout << "http web success, response: " << resPost0 << std::endl;
|
||||
@@ -263,7 +279,7 @@ void SendFileWeb(const std::string& strUrl, const std::string& localpath, const
|
||||
|
||||
// 清理
|
||||
curl_formfree(formpost); // 释放表单数据
|
||||
curl_slist_free_all(header_list); // 释放头部列表
|
||||
//curl_slist_free_all(header_list); // 释放头部列表
|
||||
curl_easy_cleanup(curl);
|
||||
} else {
|
||||
std::cerr << ">>> curl init failed" << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user