【发布时间】:2019-12-25 23:15:08
【问题描述】:
我正在尝试使用套接字构建 Web 服务器。我有 GET 请求的解析器,现在我希望能够从表单向服务器发送图像。
打印请求标头后,这是我得到的:
POST / HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------15680533759245126501880539822
Content-Length: 14006
Origin: http://127.0.0.1:8080
Connection: keep-alive
Referer: http://127.0.0.1:8080/form.html
Upgrade-Insecure-Requests: 1
-----------------------------15680533759245126501880539822
Content-Disposition: form-data; name="textline"
4213124124123123153534123412
-----------------------------15680533759245126501880539822
Content-Disposition: form-data; name="datafile"; filename="5.jpg"
Content-Type: image/jpeg
����
这是我的表格:
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="/"
enctype="multipart/form-data" method="post">
<p>
Type some text (if you like):<br>
<input type="text" name="textline">
</p>
<p>
Please specify a file, or a set of files:<br>
<input type="file" name="datafile">
</p>
<div>
<input type="submit" value="Send">
</div>
</form>
</body>
</html>
我的问题是:为什么我只接收到 4 个字节的图像,我应该怎么做才能将接收到的数据保存在 jpg 文件中?
【问题讨论】:
-
您使用的是 Windows 吗?
-
看起来您正在打印为字符串,请使用任何支持 HEX 模式的编辑器检查文件或将文件转储为十六进制,以检查第五个字节是否为零(即
'\0')。跨度> -
你说得对,我完全忘记了图像文件中有
'\0'。现在我只需要弄清楚我应该如何将它保存为图像。