将 tiff 图像转换为字符串,以作为二进制应用程序/八位字节流 (C++) 发布
Convert tiff image into String to be posted as binary application/octet-stream (C++)
我正在使用C++套接字向服务器发送包含TIFF图像的HTTP Multipart POST。此服务器需要二进制八位字节流。
我尝试的是这样的:
// Convert out data into a string that can be appended to the body
std::ifstream fin(fileName, std::ios::binary);
std::ostringstream ostrm;
ostrm << fin.rdbuf();
string data(ostrm.str());
不幸的是,我只得到 II*,而数据应该更长。我的猜测是数据包含一个 NULL 字符,这C++认为字符串已经完成。关于如何解决此问题的任何想法?
如果有帮助,这是我的身体构造代码:
string constructBody(string batchId, string fileString, string fileName) {
string body;
string CRLF = "rn";
// first we add the args
body.append("--" + string(BOUNDARY) + CRLF);
body.append("Content-Disposition: form-data; name="batch_id"" + CRLF);
body.append(CRLF);
body.append(batchId + CRLF);
body.append("--" + string(BOUNDARY) + CRLF);
// now we add the file
body.append("Content-Disposition: form-data; name="front"; filename="" + string(fileName) + """ + CRLF);
body.append("Content-Type: application/octet-stream" + CRLF);
body.append("Content-Transfer-Encording: binary" + CRLF);
body.append(CRLF);
body.append(fileString + CRLF);
body.append("--" + string(BOUNDARY) + "--" + CRLF);
body.append(CRLF);
return body;
}
这是发布代码:
string body = constructBody(batchId, data, "Front.tif");
char header[1024];
sprintf(header, "POST %s HTTP/1.1rn"
"Host: %srn"
"Content-Length: %drn"
"Connection: Keep-Alivern"
"Content-Type: multipart/form-data; boundary=%srn"
"Accept-Charset: utf-8rnrn", RECEIVER, IP, strlen(body.c_str()), BOUNDARY);
int p = send(dataSock, header, strlen(header), 0);
int k = send(dataSock, body.c_str(), strlen(body.c_str()), 0);
提前感谢!
不能使用使用 null 作为字符串终止符的函数:
int k = send(dataSock, body.c_str(), strlen(body.c_str()), 0);
您正在使用上面的strlen
。 这是不正确的。
解决此问题的方法是将length()
函数用于std::string
:
int k = send(dataSock, body.c_str(), body.length()), 0);
您在其他位置犯了相同的错误,例如在创建标头时:
"Accept-Charset: utf-8rnrn", RECEIVER, IP, strlen(body.c_str()), BOUNDARY);
应该是:
"Accept-Charset: utf-8rnrn", RECEIVER, IP, body.length(), BOUNDARY);
相关文章:
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 C++ 中的无符号字符字节流中读取值
- 警告:左移计数 >= 在 C++ 中将字节流读取为双精度变量时的类型宽度
- C++ 中的字节流
- 检查 IP 第一个八位字节是否不以 127 / 224 或 255 开头
- Microsoft加密 API 是否允许从字节流创建 ECDSA 密钥?
- 在 std::string 中使用非法的 UTF-8 八位字节作为分隔符
- 将转义的 UTF-8 八位字节的字符数组转换为 C++ 的字符串
- g++ 命令行宏定义字节流
- 使用媒体基础从h.264字节流(附件B)创建MP4容器
- 从变量中提取八位字节int64_t
- 如何从TCP字节流中提取消息的长度信息,并仅提取这么多消息
- 双值传输在字节流中
- 有没有一种简单的方法可以在Visual Studio中编写UTF-8八位字节
- 将 tiff 图像转换为字符串,以作为二进制应用程序/八位字节流 (C++) 发布
- 如何使用 PJSIP 库解码 SIP 字节流
- 通过Qt套接字发送64位整数
- 将字(32/64位)写入字符数组的原子性
- 在c++中将字节流拆分为位
- 具有非八位对齐子字段的96位长位域