WEB请求与响应

请求与响应

请求类型

  • 普通 get
    1. 参数直接附加在url后,无消息体
  • x-www-form-urlencoded
    1. 参数会放在消息体中,形式如uthCode=cc&program=_DING
  • multipart/form-data
    1. 参数会放在消息体中,每个参数会有分隔如

multipart/form-data 格式
Connection: keep-alive

----------------------------066103877560936220542177
Content-Disposition: form-data; name="ss"

dd
----------------------------066103877560936220542177--
  • application/json
    1. 参数在消息体中,可以是任意格式,一般为json
    2. 也可以是普通的字符串
Content-Length: 58

{
    "authCode": "cc",
    "program": "TDING"
}
  • ajax请求
    1. 格式如x-www-form-urlencoded
    2. 在请求头中存在X-Requested-With: XMLHttpRequest

下载操作

  • 文件下载仅需要将文件字节流写入到response中即可
  • 过程:
    1. 构建响应头
    2. 获取到文件字节流
    3. 读取字节流到数组中,并将字节数组写入到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);
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值