【发布时间】:2021-07-19 03:58:39
【问题描述】:
想通过 RabbitMQ-C 发送图像,但图像文件太大。接收方无法检索图像。因此,我将图像转换为 base64,然后将其放入 JSON。
const char *msg;
FILE *image1;
if (image1 = fopen(path, "rb")) {
fseek(image1, 0, SEEK_END); //used to move file pointer to a specific position
// if fseek(0 success, return 0; not successful, return non-zero value
//SEEK_END: end of file
length = ftell(image1); //ftell(): used to get total size of file after moving the file pointer at the end of the file
sprintf(tmp, "size of file: %d bytes", length);
//convert image to base64
std::string line;
std::ifstream myfile;
myfile.open(path, std::ifstream::binary);
std::vector<char> data((std::istreambuf_iterator<char>(myfile)), std::istreambuf_iterator<char>() );
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len);
std::string code = base64_encode((unsigned char*)&data[0], (unsigned int)data.size());
//convert std::string to const char
const char* base64_Image = code.c_str();
json j ;
j.push_back("Title");
j.push_back("content");
j.push_back(base64_Image);
std::string sa = j.dump();
msg = sa.c_str(); //convert std::string to const char*
}
else {
return;
}
使用 RabbitMQ-C 向接收者发送消息(msg)但失败 [错误指向这里]
是 const char* 不能使用 amqp_cstring_bytes(msg) 转换为 amqp_bytes_t 吗??
respo = amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(routing_key),0, 0, NULL, amqp_cstring_bytes(msg));
得到这个错误
If there is a handler for this exception, the program may be safely continued.```
Anyone know how to send image as JSON using RabbitMQ-C & C++ ?
【问题讨论】:
-
您如何确定图片“太大”而无法接收?如果这真的是问题所在,base64 编码的东西会增加 33% 的大小,所以这只会加剧问题。
-
感谢@Botje 的回复。实际上,我不太确定是什么原因导致接收器无法解码我发送的图像。我想发送的图像主要在 400kb 左右,但我的 base64 字符串超过 50k 个字符。以前我使用 RapidJSON 创建 JSON 对象
Write.String(base64,255, true);但字符超过 255。现在我使用 nlohmann json ,它允许我在 JSON 中创建和获取完整的 base64 字符串,但无法通过 RabbitMQ 发送。你知道我在代码中犯了什么错误吗?
标签: c++ mfc base64 nlohmann-json rabbitmq-c