如:<form name=“dd” method=“post” action=“some.jsp”> Name:<input type=“text” name=“name”/> <input type=“submit” value=“提交”/> </form>
Accept:*/* //客户端支持的返回类型
Referer:http://localhost:2046/b.html //客户端的网址
Accept-Language:zh-cn //客户端的语言类型
User-Agent:Mozilla/4.0 //客户端的浏览器类型
Accept-Encoding:gzip,deflate //客户端支持的数据压缩类型
Host:localhost:2046 //服务器的地址
Connection:Keep-Alive //是否保持连接,可选的值为Keep-alive|close
Cookie: //缓存在客户端的数据(数据量最大4K)
Content-Length: 8 //发送的字节长度
Cache-Control: no-cache //是否缓存数据
Content-Type: application/x-www-form-urlencoded //内容类型,此意为表单
[必须的一行空行]
name=itcast&pwd=1234 //请求的正文,如果是POST请求则
<span style="font-size: 18px; white-space: pre;"> </span><span style="font-size:18px;">//1、声明输出流 <span style="white-space: pre;"> </span>ByteArrayOutputStream out = new ByteArrayOutputStream(); <span style="white-space: pre;"> </span>//2、声明压缩流 <span style="white-space: pre;"> </span>GZipOutputStream zipout = new GZipOutputStream(out); <span style="white-space: pre;"> </span>//3、向输出流压缩数据,此时数据写入到out中 <span style="white-space: pre;"> </span>zipout.write(“someString”.getBytes(“utf-8”)); <span style="white-space: pre;"> </span>Zipout.close(); //不要忘了关闭 <span style="white-space: pre;"> </span>//4、向客户端发送数据 <span style="white-space: pre;"> </span>设置响应头: <span style="white-space: pre;"> </span>resp.setHeader("Content-Encoding","gzip"); <span style="white-space: pre;"> </span>resp.setHeader("Content-Length",""+b.length); <span style="white-space: pre;"> </span>resp.setHeader("Content-Type","text/html;charset=UTF-8"); <span style="white-space: pre;"> </span>response.getOutputStream().write(out.toByteArray()); <span style="white-space: pre;"> </span>//使用场景: <span style="white-space: pre;"> </span>//限制流量的客户端</span>
<span style="font-size: 18px; white-space: pre;"> </span><span style="font-size:18px;">resp.setContentType("application/force-download"); <span style="white-space: pre;"> </span>resp.setHeader("Content-Disposition","attachment;filename=\""+name+"\""); <span style="white-space: pre;"> </span>//如果是中文要进行编码 <span style="white-space: pre;"> </span>String name = "我的图片.jpg“; <span style="white-space: pre;"> </span>name = URLEncoder.encode(name,"UTF-8"); <span style="white-space: pre;"> </span>//以下使用IO读取文件 <span style="white-space: pre;"> </span>InputStream in = new FileInputStream(path); <span style="white-space: pre;"> </span>byte[] b = new byte[1024]; <span style="white-space: pre;"> </span>int len = 0; <span style="white-space: pre;"> </span>OutputStream out = resp.getOutputStream(); <span style="white-space: pre;"> </span> while((len=in.read(b))!=-1){ <span style="white-space: pre;"> </span> out.write(b,0,len); <span style="white-space: pre;"> </span>}</span><span style="font-size: 18px;"> </span>