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

发布于 2024-11-28 09:21:14 字数 1256 浏览 4 评论 0原文

所以我尝试这样的代码:

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 作为文件名,而它应该返回从 Demonoid.com.txt 下载的 Torrent。如何修复我的文件上传 http 请求文件名解析器?

So I try such code:

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();

But it outputs in for example:

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

This means that from filename="Torrent downloaded from Demonoid.com.txt" my cede returnes Torrent d as a file name while it should return Torrent downloaded from Demonoid.com.txt. How to fix my file upload http request filename parser?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃安静 2024-12-05 09:21:14

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

string::find returns the index of the first character in the search string. So it's giving you the index of the f in filename= when you search for that.

In the line

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

You'll have to change that to

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 "

Then change

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

To

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

You might want to add another variable to quit having to add 10 all the time as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文