[英]How to upload file through HTTP protocol with C++?
I'm going to upload files with C++, to a Java spring boot web server.我将使用 C++ 将文件上传到 Java spring 启动 Z2567A5EC9705EB7AC2ZC9 服务器。
I constructed the protocol looks like below (in plain text):我构建的协议如下所示(纯文本):
POST /gallery/multimedia/uploadvideo HTTP/1.1
Host: 192.168.0.133:8981
Content-Type: multipart/form-data; boundary=------boundary1804289383
Connection: keep-alive
--------boundary1804289383
Content-Type: video/mp4
Content-Disposition: form-data; name="file"; filename="1.mp4"
Content-Length: 948611
Content-Transfer-Encoding: 8bit
... binary video data here ...
--------boundary1804289383--
The server side is Java spring boot server, interface defined as below:服务器端为 Java spring 启动服务器,接口定义如下:
@PostMapping("uploadvideo")
public ResultVo uploadVideo(@RequestParam("file") MultipartFile file);
While posting file, the server responses with code 400, and complains that发布文件时,服务器以代码 400 响应,并抱怨
Required request part 'file' is not present
所需的请求部分“文件”不存在
However with a simple HTML page, file uploaded successfully, the HTML page listed below:但是通过一个简单的 HTML 页面,文件上传成功,HTML 页面如下:
<html>
<head></head>
<body>
<form id="form" enctype="multipart/form-data" method="post"
action="http://192.168.0.133:8981/gallery/multimedia/uploadvideo">
<input id="file" name="file" type="file" />
<input id="submit" value="submit" type="submit" />
</form>
</body>
</html>
What do I miss?我想念什么?
Edit:编辑:
I've tried Postman/Chrome console/curl, all these tools only print the request looks like:我试过 Postman/Chrome 控制台/curl,所有这些工具只打印请求,如下所示:
# this is from curl
POST /gallery/multimedia/uploadvideo HTTP/1.1
User-Agent: curl/7.29.0
Host: 192.168.0.133:8981
Accept: */*
Content-Length: 187
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------3c653d03b97f
How should I construct the file part?我应该如何构建文件部分? Any ideas?
有任何想法吗?
Firstly, thanks for @strupo 's suggestion.首先,感谢@strupo 的建议。
By turning on --trace
option of curl
and viewing the output log file, I finally fixed this problem.通过打开
curl
的--trace
选项,查看output日志文件,终于解决了这个问题。
In curl
, it posts file by several packets:在
curl
中,它通过几个数据包发布文件:
The header: header:
POST /gallery/multimedia/uploadvideo HTTP/1.1
User-Agent: curl/7.29.0
Host: 192.168.0.133:8981
Accept: */*
Content-Length: 13602 # you have to calculate content length first
Expect: 100-continue # very important line
Content-Type: multipart/form-data; boundary=------3c653d03b97f
then it waits for server response:然后它等待服务器响应:
HTTP/1.1 100
after server responsed code 100, it sends data content, the form-data header goes first:服务器响应代码 100 后,它发送数据内容,表单数据 header 先行:
--------3c653d03b97f
Content-Type: video/mp4
Content-Disposition: form-data; name="file"; filename="1.mp4"
Content-Length: 6640
Content-Transfer-Encoding: 8bit
and file content follows, (in my case, a large memory is allocated, read from file and write to socket in once), and next file.然后是文件内容(在我的情况下,分配了一个大的 memory,从文件中读取并写入一次套接字),以及下一个文件。
Finally the protocol should finished with boundary line:最后,协议应该以边界线结束:
--------3c653d03b97f--
The Content-Length
should include all bytes being sent after header. Content-Length
应包括在 header 之后发送的所有字节。 The boundary
should be prefixed with --
in file part. boundary
应在文件部分以--
为前缀。 Also the \r\n
everywhere should be noticed.还应注意各处的
\r\n
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.