https://blog.csdn.net/shentian885/article/details/108541881
//添加头部信息
resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"名字"</span><span class="token punctuation">,</span><span class="token string">"值"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//添加该名字与值</span>
<span class="token keyword">boolean</span> falg <span class="token operator">=</span> resp<span class="token punctuation">.</span><span class="token function">containsHeader</span><span class="token punctuation">(</span><span class="token string">"名字"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//查询响应头中是否包含某个名字,存在返回true,否则返回 false</span>
//添加响应头时,名字可以随便取,但是一般使用常见的名字
<span class="token comment">//Server -> tomcat版本</span>
<span class="token comment">//Date -> 时间</span>
resp<span class="token punctuation">.</span><span class="token function">setDateHeader</span><span class="token punctuation">(</span><span class="token string">"Date"</span><span class="token punctuation">,</span><span class="token keyword">long</span><span class="token operator">-</span>时间的毫秒数<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//与resp.addHeader("Date","值")效果相同</span>
<span class="token comment">//Content-Type -> 响应返回的内容类型 </span>
resp<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">"text/html;charset=utf-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//与resp.addHeader("Content-Type","text/html;charset=utf-8")效果相同</span>
<span class="token comment">//Location -> 302时,返回请求资源的新路径</span>
<span class="token comment">//Content-Encoding -> 服务端能够发送压缩编码类型</span>
resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Content-Encoding"</span><span class="token punctuation">,</span><span class="token string">"gzip"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//Content-Length -> 服务器发送的压缩数据的长度</span>
resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Content-Length"</span><span class="token punctuation">,</span><span class="token string">"长度"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//Content-language -> 服务器发送的语言类型</span>
resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Content-Language"</span><span class="token punctuation">,</span><span class="token string">"zh-cn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//Last-Modified -> 服务端对该资源最后的修改时间</span>
resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Last-Modifies"</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//Content-Disposition -> 服务器要求客户端以下载文件的方式打开该文件</span>
resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Content-Disposition"</span><span class="token punctuation">,</span><span class="token string">"attachment;filename=下载文件名.后缀名"</span><span class="token punctuation">)</span>
<span class="token comment">//设置浏览器是否缓存</span>
resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Expires"</span><span class="token punctuation">,</span><span class="token string">"-1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Cache-Control"</span><span class="token punctuation">,</span><span class="token string">"no-cache"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
resp<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Pragma"</span><span class="token punctuation">,</span><span class="token string">"no-cache"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//针对不同浏览器的兼容</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42