fix file upload
This commit is contained in:
@@ -25,7 +25,7 @@
|
|||||||
#include "interface.h"
|
#include "interface.h"
|
||||||
#include "rocketmq.h"
|
#include "rocketmq.h"
|
||||||
#include "nlohmann/json.hpp"
|
#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) {
|
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 = curl_easy_init();
|
CURL* curl = curl_easy_init();
|
||||||
if (curl) {
|
if (curl) {
|
||||||
|
// ★新增:错误缓冲,便于看到更具体的错误
|
||||||
|
char errbuf[CURL_ERROR_SIZE] = {0};
|
||||||
|
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); // ★新增
|
||||||
|
|
||||||
// 设置请求 URL 和 POST 请求
|
// 设置请求 URL 和 POST 请求
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
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* formpost = nullptr;
|
||||||
curl_httppost* lastptr = 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,
|
curl_formadd(&formpost, &lastptr,
|
||||||
CURLFORM_COPYNAME, "file",
|
CURLFORM_COPYNAME, "file",
|
||||||
CURLFORM_FILE, localpath,
|
CURLFORM_FILE, localpath.c_str(),
|
||||||
CURLFORM_END);
|
CURLFORM_END);
|
||||||
|
|
||||||
// 添加 `path` 字段
|
// 添加 `path` 字段
|
||||||
curl_formadd(&formpost, &lastptr,
|
curl_formadd(&formpost, &lastptr,
|
||||||
CURLFORM_COPYNAME, "path",
|
CURLFORM_COPYNAME, "path",
|
||||||
CURLFORM_COPYCONTENTS, cloudpath,
|
CURLFORM_COPYCONTENTS, cloudpath.c_str(),
|
||||||
CURLFORM_END);
|
CURLFORM_END);
|
||||||
|
|
||||||
// 添加 `isReserveName` 字段
|
// 添加 `isReserveName` 字段
|
||||||
@@ -238,13 +252,14 @@ void SendFileWeb(const std::string& strUrl, const std::string& localpath, const
|
|||||||
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
||||||
|
|
||||||
// 设置头信息
|
// 设置头信息
|
||||||
struct curl_slist* header_list = nullptr;
|
//struct curl_slist* header_list = nullptr;
|
||||||
header_list = curl_slist_append(header_list, "Content-Type: multipart/form-data");
|
//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_HTTPHEADER, header_list);
|
||||||
|
|
||||||
// 设置超时时间
|
// 设置超时时间
|
||||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
|
||||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); // ★新增:避免多线程环境下超时触发信号
|
||||||
|
|
||||||
// 设置写入响应数据的函数
|
// 设置写入响应数据的函数
|
||||||
std::string resPost0;
|
std::string resPost0;
|
||||||
@@ -254,7 +269,8 @@ void SendFileWeb(const std::string& strUrl, const std::string& localpath, const
|
|||||||
// 执行请求
|
// 执行请求
|
||||||
CURLcode res = curl_easy_perform(curl);
|
CURLcode res = curl_easy_perform(curl);
|
||||||
if (res != CURLE_OK) {
|
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);
|
DIY_ERRORLOG("process","【ERROR】前置上传暂态录波文件 %s 失败,请检查文件上传接口配置",localpath);
|
||||||
} else {
|
} else {
|
||||||
std::cout << "http web success, response: " << resPost0 << std::endl;
|
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_formfree(formpost); // 释放表单数据
|
||||||
curl_slist_free_all(header_list); // 释放头部列表
|
//curl_slist_free_all(header_list); // 释放头部列表
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
} else {
|
} else {
|
||||||
std::cerr << ">>> curl init failed" << std::endl;
|
std::cerr << ">>> curl init failed" << std::endl;
|
||||||
|
|||||||
@@ -24,12 +24,11 @@
|
|||||||
#include "log4cplus/fileappender.h"
|
#include "log4cplus/fileappender.h"
|
||||||
#include "log4cplus/layout.h"
|
#include "log4cplus/layout.h"
|
||||||
#include "log4cplus/ndc.h"
|
#include "log4cplus/ndc.h"
|
||||||
#include "log4cplus/log4.h"
|
|
||||||
#include "log4cplus/spi/loggingevent.h"
|
#include "log4cplus/spi/loggingevent.h"
|
||||||
|
|
||||||
#include "rocketmq.h"
|
#include "rocketmq.h"
|
||||||
#include "interface.h"
|
#include "interface.h"
|
||||||
#include "log4cplus/log4.h"
|
#include "log4.h"
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
//log4命名空间
|
//log4命名空间
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include "interface.h" //用于访问接口
|
#include "interface.h" //用于访问接口
|
||||||
#include "log4cplus/log4.h" //用于日志
|
#include "log4.h" //用于日志
|
||||||
#include "curl/curl.h" //用于访问接口
|
#include "curl/curl.h" //用于访问接口
|
||||||
#include "nlohmann/json.hpp" //用于构造json
|
#include "nlohmann/json.hpp" //用于构造json
|
||||||
#include "worker.h" //shell接口
|
#include "worker.h" //shell接口
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
#include "rocketmq.h"
|
#include "rocketmq.h"
|
||||||
#include "nlohmann/json.hpp"
|
#include "nlohmann/json.hpp"
|
||||||
#include "log4cplus/log4.h"
|
#include "log4.h"
|
||||||
#include "interface.h"
|
#include "interface.h"
|
||||||
#include "front.h"
|
#include "front.h"
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
#include "worker.h"
|
#include "worker.h"
|
||||||
#include "interface.h"
|
#include "interface.h"
|
||||||
#include "rocketmq.h"
|
#include "rocketmq.h"
|
||||||
#include "log4cplus/log4.h"
|
#include "log4.h"
|
||||||
#include "front.h"
|
#include "front.h"
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user