HTTP知多少——Content-disposition(文件下載)
HTTP知多少——Content-Type(內(nèi)容類型)詳解
在Http請求中,我們每天都在使用Content-type來指定不同格式的請求信息。
鏈接:Postman資源下載。一款神器喲~
提取碼:qmph
https://blog.csdn.net/wangjun5159/article/details/49644507
1. Content-Type
MediaType,即Internet Media Type,互聯(lián)網(wǎng)媒體類型;也叫做MIME類型,在Http協(xié)議消息頭中,使用Content-Type來表示具體請求中的媒體類型信息。
類型格式:type/subtype(;parameter)?type;
主類型:任意的字符串,如text,如果是*號代表所有;
subtype子類型:任意的字符串,如html,如果是*號代表所有;
parameter:(可選)一些參數(shù),如Accept請求頭的參數(shù),Content-Type的charset參數(shù)。
例如:Content-Type:text/html;charset:utf-8;
(敲黑板,劃重點(diǎn))小伙伴們提出疑問,Content-Type和Accent有什么區(qū)別呢?
- Accept代表發(fā)送端(客戶端)希望接受到的數(shù)據(jù)類型。比如:Accept:text/xml;代表客戶端希望接受到的數(shù)據(jù)類型是xml。
- Content-type代表發(fā)送端(客戶端|服務(wù)器)發(fā)送的實(shí)體的數(shù)據(jù)類型。比如:Content-Type:text/html;代表發(fā)送端發(fā)送的數(shù)據(jù)格式是html。
- 二者結(jié)合起來,
request:Accept:text/xml;Content-Type:text/html,即代表希望接受到的數(shù)據(jù)類型是xml,本次請求發(fā)送的數(shù)據(jù)類型格式是html;
1.2 常見的媒體格式
- text/html:HTML格式
- text/plain:純文本格式
- text/XML:XML格式
- image/gif:gif圖片格式
- image/jped:jpg圖片格式
- image/png:png圖片格式
以application開頭的媒體格式類型:
- application/xhtml+xml:XHTML格式
- application/xml:XML數(shù)據(jù)格式
- application/atom+xml:Atom XML聚合格式
- application/json:JSON數(shù)據(jù)格式【常用】
- application/pdf:pdf數(shù)據(jù)格式
- application/msword:Word文檔格式
- application/octet-stream:二進(jìn)制流格式(如常見的文件下載)
- application/x-www-form-urlencoded:<form encType=" ">中默認(rèn)的encType,form表單數(shù)據(jù)編碼為key/value格式發(fā)送到服務(wù)器(表單默認(rèn)的提交數(shù)據(jù)格式)【常用】
另外一種常見的媒體格式是上傳文件時(shí)使用的:【常用】
- multipart/form-data: ['m?lti:pɑ:t] 需要在表單進(jìn)行文件上傳時(shí),就需要使用該格式。
1.2 SpringMVC中關(guān)于Content-Type類型的使用
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
@AliasFor("path")
String[] value() default {};
@AliasFor("value")
String[] path() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
參數(shù)詳情:
- value:指定請求的實(shí)際地址,比如/action/info之類。
- method:指定請求的method方法,GET、POST、PUT、DELETE、PATCH等。
-
consumes:指定請求處理的提交內(nèi)容類型(Content-Type),例如
application/json,text/html; - produces:指定返回的內(nèi)容類型,僅當(dāng)request請求頭中的(Accept)類型包含該指定類型才返回。
- params:指定request中必須包含某些參數(shù)值,才讓該方法處理請求。
- headers:指定request中必須包含某些指定的header值,才讓該方法處理請求。
需要注意的是:
consumes、produces使用content-type信息進(jìn)行過濾信息;headers中使用content-type進(jìn)行過濾和判斷。
1.3 使用示例
1. headers的示例
//Referer表示http請求的來源
@RequestMapping(value = "test/headers",method = RequestMethod.GET,
headers = "Referer=http://www.baidu.com")
public String testHeaders(){
return "success";
}
使用headers元素設(shè)置特定的請求頭。http請求頭中Referer的含義和作用

2. params的示例
@RequestMapping(value = "test/params",method = RequestMethod.GET,
params = "myParam=myValue")
public String testParams(){
return "success";
}
服務(wù)器僅理解請求中包含myParam,值為myValue的請求,起到一個(gè)過濾的作用。

3. consumes的示例
consumes限定request中的Content-Type為application/json類型。即服務(wù)器只接受json格式!
@RequestMapping(value = "test/consumes",method = RequestMethod.POST,
consumes = "application/json")
public String testConsumes(String message){
System.out.println("獲取到的json:"+message);
return "success";
}


4. produces的示例
produces作用就是處理request請求中Accept頭中包含application/json的請求,同時(shí)設(shè)置返回的Content-Type是application/json。
@RequestMapping(value = "test/produces",method = RequestMethod.GET,
produces = "application/json")
public String testProduces(){
return "success";
}

1.5 Accept頭的規(guī)則
我們在上面可以看到,Accept是客戶端希望接受的格式,我們可以看到不僅一個(gè)格式,那么他們之間的區(qū)別是什么呢?
- Accept:text/html;application/xml;application/json
按照出現(xiàn)的順序和produces進(jìn)行匹配; - Accept:application/xml;q=0.5,application/json;q=0.9,text/html
將按照如下順序進(jìn)行produces的匹配 ①text/html ②application/json ③application/xml參數(shù)為媒體類型的質(zhì)量因子,越大則優(yōu)先權(quán)越高(從0到1) - Accept:/,text/,text/html
將按照如下順序進(jìn)行produces的匹配 ①text/html ②text/ ③/。即最明確的優(yōu)先匹配。
參考文章:
Http請求中Content-Type講解以及在Spring MVC注解中produce和consumes配置詳解