【问题标题】:Serialize PNG and Send to Rest Client序列化 PNG 并发送到 Rest 客户端
【发布时间】:2016-02-27 19:41:15
【问题描述】:

我正在向我的其他客户发送 png 格式的图像。 但是当我在客户端下载文件并尝试打开它时,我会得到错误。 我相信文件的标头丢失了,但是如何将它们添加到服务器响应中?

On the details of my picture i can see, that the size and so on is missing.

ifstream Stream;

Stream.open(FullFileName,std::ios::binary);
string Content, Line;

if (Stream)
{
    while (getline(Stream,Line)){
        Content += Line;
    }
}

Stream.close();
Request::request->set_body(Content);

【问题讨论】:

  • getline 在读取二进制文件时是否有意义?
  • 你会用什么?如果我不打开二进制读取,我的代码只会读取图像的第一行。
  • 这是你的代码问题,也是我自己学习的问题。我认为将 getLine 与二进制一起使用是没有意义的,因为“行”可能包含 1 个字节或整个文件,具体取决于其内容。我通常以标题指定的大小读取二进制文件,或者至少以常规大小的块读取。但是,我不是 C++ 专家,所以你或其他人对此有意见我很好奇。

标签: c++ rest png


【解决方案1】:

搜了一圈发现这边有post的帖子。

以及我由此创建的序列。

ifstream *Stream = new ifstream(FullFileName, std::ios::in | std::ios::binary);

    std::ostringstream *oss = new ostringstream();
    *oss << Stream->rdbuf();
    string *Content = new string(oss->str());

    Stream->close();
    Request::request->set_body(*Content);

【讨论】:

    猜你喜欢
    • 2022-06-15
    • 2016-12-14
    • 2017-04-26
    • 2015-03-13
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多