rabbitmq-c 发送 nlohmann json
rabbitmq-c send nlohmann json
想通过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)给接收者失败
[错误指向这里]
is 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++ ?
amqp_cstring_bytes
需要一个 C 字符串,它通常以 NUL 字节结尾。您的 PNG 文件几乎可以保证包含一个 NUL 字节,这就解释了为什么您的消息在中途被截断了。
至于您粘贴的代码:由 sa.c_str()
编辑的指针 return 仅在 sa
处于活动状态且未修改时才有效。一旦退出包含 sa
的定义的块,变量就死了并被掩埋了。
相反,使用 amqp_bytes_alloc
和 return 获得适当大小的缓冲区:
amqp_bytes_t bytes = amqp_bytes_malloc(sa.length());
strncpy((char *)bytes.bytes, sa.c_str(), sa.length());
然后将 bytes
对象传递给 amqp_basic_publish
。完成后别忘了 ampqp_bytes_free
。
想通过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)给接收者失败 [错误指向这里]
is 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++ ?
amqp_cstring_bytes
需要一个 C 字符串,它通常以 NUL 字节结尾。您的 PNG 文件几乎可以保证包含一个 NUL 字节,这就解释了为什么您的消息在中途被截断了。
至于您粘贴的代码:由 sa.c_str()
编辑的指针 return 仅在 sa
处于活动状态且未修改时才有效。一旦退出包含 sa
的定义的块,变量就死了并被掩埋了。
相反,使用 amqp_bytes_alloc
和 return 获得适当大小的缓冲区:
amqp_bytes_t bytes = amqp_bytes_malloc(sa.length());
strncpy((char *)bytes.bytes, sa.c_str(), sa.length());
然后将 bytes
对象传递给 amqp_basic_publish
。完成后别忘了 ampqp_bytes_free
。