请求与响应
请求类型
- 普通 get
- 参数直接附加在url后,无消息体
- x-www-form-urlencoded
- 参数会放在消息体中,形式如
uthCode=cc&program=_DING
- multipart/form-data
- 参数会放在消息体中,每个参数会有分隔如
multipart/form-data 格式
Connection: keep-alive
----------------------------066103877560936220542177
Content-Disposition: form-data; name="ss"
dd
----------------------------066103877560936220542177--
- application/json
- 参数在消息体中,可以是任意格式,一般为json
- 也可以是普通的字符串
Content-Length: 58
{
"authCode": "cc",
"program": "TDING"
}
- ajax请求
- 格式如x-www-form-urlencoded
- 在请求头中存在X-Requested-With: XMLHttpRequest
下载操作
- 文件下载仅需要将文件字节流写入到response中即可
- 过程:
- 构建响应头
- 获取到文件字节流
- 读取字节流到数组中,并将字节数组写入到response中
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(pictrue.getFileName(), "UTF-8"));
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = response.getOutputStream();
bis = new BufferedInputStream(new ByteArrayInputStream(pictrue.getByteStr().getBytes()));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
bis.close();
} catch (IOException e) {
logger.error(e);
}