`

http-post-upload 格式

阅读更多

目的:发起一个携带文件和字段的http请求,理解http 通过 boundary分割域 实现文件上传功能

 

浏览器:

 

 

客户端发送的请求:

http://dl2.iteye.com/upload/attachment/0124/6600/0e51b48c-9d8f-35b8-ab95-9b0727d0e7c4.png

结论:

http 文件上传,http协议通过定义:boundary=---------------------------7e131a1ade2200 来实现每个字段域直接的分割

针对普通的文本域,比如:username,password等 

 

-----------------------------7e131a1ade2200
Content-Disposition: form-data; name="username"

xinchun.wang
-----------------------------7e131a1ade2200

 

针对文件上传的文本文件格式为:

-----------------------------7e131a1ade2200
Content-Disposition: form-data; name="pics1"; filename="a.txt"
Content-Type: text/plain

com_qunar_mall_gtt,gtt.mall.qunar.com,AFARE
-----------------------------7e131a1ade2200

 

针对图片上传格式同文件:



 

以上是通过wireshark 查看的客户端请求的报文

 

后记:

apache common fileupload 等组件 就是通过把 ---------------------------7e131a1ade2200 \r\n 转换为 二进制数组,然后根据request 返回的InputStream 逐个字节的匹配,以boundary的二进制内容为分割点,逐步解析的。

核心实现类:org.apache.commons.fileupload.MultipartStream

  • 大小: 13.5 KB
  • 大小: 30.8 KB
1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics