当前位置:网站首页>为什么图片传输要使用base64编码
为什么图片传输要使用base64编码
2022-07-04 12:51:00 【诗人不写诗】
一、背景知识
一般使用HTTP协议,传输的是字符文本或二进制数据,这2种场景覆盖了几乎所有使用HTTP协议的场景。HTTP协议规定,使用Content-Type请求头来约定传输消息的类型,这决定了请求方如何组织数据(编码和格式)以及接收方如何解析数据。Content-Type常用的取值有如下几种:
1、application/x-www-form-urlencoded
这种方式发送的数据在url上拼接,以key1=val1&key2=val2 的方式进行编码,key和val都进行了 URL转码,这是一种表单提交方式,表单意思是有多个值。
2、multipart/form-data
从名字上看,传输的数据是多份的,且是表单的格式,这种方式其实是一个功能强大的方式,几乎可以满足所有数据传输要求,只是使用起来不太直接,所以我们只使用这种方式来提交文件,因为文件只能以字节流的形式传输,以传输一个文件为例,需要一个key,可以命名为myFile,然后需要定义value 类型和内容,类型是fille,内容就是文件字节流。服务方需要先根据myFIle取出文件,然后根据类型解析。
3、application/json
这种方式将数据以json格式组织,放在body里发送。
4、text/xml
这种方式将数据以xml格式组织,放在body里发送,这种格式逐渐被json取代,因为,json格式更加简洁明了。
二、实际使用
我们在传输图片时,有可能是前后端交互,有可能是2个后端交互,处理方式通常不同,由于前端负责界面交互,在发送HTTP请求时通常具备丰富的Content-Type,所以前端在发送图片时,就会使用multipart/form-data这种方式,这也是前后端默认的规则。
但是在2个后端进行交互时,默认的数据格式是application/json,所以对图片这种数据就要进行转化,通常,我们在得到了图片的字节流byte[ ]后,不会在json中直接传输byte[ ],因为担心传输过程中,有服务对其进行隐式编码然后解码,这就导致字节流发生了变化,所以我们在传输源头就进行base64编码,因为base64使用的ASCII编码是所有编码方式都支持的,即使传输过程中有服务对其进行编解码,也能原样还原。所以使用base64编码是一种安全策略,但是base64会导致体积增大约三分之一。
另外,base64也是浏览器直接支持的一种格式。如下可以直接展示。
边栏推荐
- 【C 题集】of Ⅶ
- R语言使用dplyr包的mutate函数对指定数据列进行标准化处理(使用mean函数和sd函数)并基于分组变量计算标准化后的目标变量的分组均值
- The Secretary of Homeland Security warned immigrants "not to embark on a dangerous journey"
- Basic mode of service mesh
- Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
- Code hoof collection of wonderful secret place
- 以房抵债能否排除强制执行
- 忠诚协议是否具有法律效力
- Excel快速合并多行数据
- Five "potential errors" in embedded programming
猜你喜欢
Interview disassembly: how to check the soaring usage of CPU after the system goes online?
【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value
Introducing testfixture into unittest framework
The font of markdown grammar is marked in red
使用默认路由作为指向Internet的路由
Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire
MySQL version 8 installation Free Tutorial
JVM memory layout detailed, illustrated, well written!
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
随机推荐
程序员的焦虑
安装Mysql
CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
The Secretary of Homeland Security warned immigrants "not to embark on a dangerous journey"
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
奇妙秘境 码蹄集
R语言使用dplyr包的mutate函数对指定数据列进行标准化处理(使用mean函数和sd函数)并基于分组变量计算标准化后的目标变量的分组均值
JVM memory layout detailed, illustrated, well written!
go语言中的文件创建,写入,读取,删除(转)
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
Programmer anxiety
File creation, writing, reading, deletion (transfer) in go language
392. Judgement subsequence
【C 题集】of Ⅶ
sharding key type not supported
Detailed explanation of Fisher information quantity detection countermeasure sample code
锐成芯微冲刺科创板:年营收3.67亿拟募资13亿 大唐电信是股东
吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
Unittest框架中引入TestFixture
Unity shader learning (3) try to draw a circle