【问题标题】:rabbitmq-c send nlohmann jsonrabbitmq-c 发送 nlohmann json
【发布时间】: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


【解决方案1】:

amqp_cstring_bytes 需要一个 C 字符串,通常以 NUL 字节结束。您的 PNG 文件几乎可以保证包含一个 NUL 字节,这就解释了为什么您的消息在中途被截断了。

至于您粘贴中的代码:sa.c_str() 返回的指针仅在sa 处于活动状态且未修改时才有效。一旦退出包含sa 定义的块,变量就死了。

相反,使用amqp_bytes_alloc 获取适当大小的缓冲区并返回:

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

【讨论】:

  • 我已经尝试过了,我在使用Error C2664 'char *strncpy(char *,const char *,std::size_t)': cannot convert argument 1 from 'void *' to 'char *' 时收到此错误strncpy(bytes.bytes, sa.c_str(), sa.length());
  • 您需要添加演员表才能消除该错误。见编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多