gpt4 book ai didi

c++ - 如何从http请求正文字符串中获取文件名?

转载 作者:行者123 更新时间:2023-11-28 01:04:37 27 4
gpt4 key购买 nike

所以我尝试这样的代码:

std::ofstream myfile;
myfile.open ("example.txt", std::ios_base::app );
myfile << "Request body: " << request->body << std::endl << "Request size: " << request->body.length() << std::endl;

size_t found_file = request->body.find("filename=");
if (found_file != std::string::npos)
{
size_t end_of_file_name = request->body.find("\"",found_file + 1);
if (end_of_file_name != std::string::npos)
{
std::string filename(request->body, found_file+10, end_of_file_name - found_file);
myfile << "Filename == " << filename << std::endl;
}
}
myfile.close();

但是它输出例如:

Request body: ------WebKitFormBoundary0tbfYpUAzAlgztXL

Content-Disposition: form-data; name="datafile"; filename="Torrent downloaded from Demonoid.com.txt"

Content-Type: text/plain



Torrent downloaded from http://www.Demonoid.com

------WebKitFormBoundary0tbfYpUAzAlgztXL--


Request size: 265
Filename == Torrent d

这意味着从 filename="Torrent downloaded from Demonoid.com.txt" 我的 cede 返回 Torrent d 作为文件名,而它应该返回 Torrent从 Demonoid.com.txt 下载。如何修复我的文件上传 http 请求文件名解析器?

最佳答案

string::find 返回搜索字符串中第一个字符的索引。因此,当您搜索时,它会为您提供 filename=f 的索引。

行内

size_t  end_of_file_name = request->body.find("\"",found_file + 1);

你必须把它改成

size_t  end_of_file_name = request->body.find("\"", found_file + 9 + 1); // 9 because that's the length of "filename=" and 1 to start at the character after the "

然后改变

std::string filename(request->body, found_file+10, end_of_file_name - found_file);

std::string filename(request->body, found_file + 10, end_of_file_name - (found_file + 10));

您可能想要添加另一个变量,从而避免一直添加 10

关于c++ - 如何从http请求正文字符串中获取文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6967733/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com