一个请求行、若干消息头、以及实体内容,以下所示 :html
HTTP响应:java
格式: HTTP版本号 状态码 缘由叙述<CRLF>web
举例:HTTP/1.1 200 OK浏览器
状态码缓存 |
含义服务器 |
100~199post |
表示成功接收请求,要求客户端继续提交下一次请求才能完成整个处理过程测试 |
200~299this |
表示成功接收请求并已完成整个处理过程,经常使用200url |
300~399 |
为完成请求,客户需进一步细化请求。例如,请求的资源已经移动一个新地址,经常使用302(你请求我我没有让你去找别人(location))、307和304(让你去拿缓存的数据) |
400~499 |
客户端的请求有错误,经常使用404 |
500~599 |
服务器端出现错误,经常使用 500 |
package com.servlet.cn; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { test4(response); } /* * 测试Content-Disposition头, 控制浏览器如下载的方式打卡图片 */ private void test4(HttpServletResponse response) throws IOException { response.setHeader("Content-Disposition", "attachment; filename=1.jpg"); InputStream in = this.getServletContext().getResourceAsStream("/1.jpg"); int len; byte[] b = new byte[1024]; OutputStream out = response.getOutputStream(); while ((len = in.read(b)) > 0) { out.write(b, 0, len); } } /* * 测试refresh头 ,注册完成以后通过几秒跳到主页 */ private void test3(HttpServletResponse response) throws IOException { //response.setHeader("Refresh", "3"); //每一个三秒刷新该页面一次 response.setHeader("refresh", "3;url=http://www.baidu.com"); //格三秒以后转到百度首页 String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; response.getOutputStream().write(data.getBytes()); } /* * 测试Content-Encoding 头, 服务器压缩后输出到客户端 */ private void test2(HttpServletResponse response) throws IOException { System.out.println("lala"); String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; System.out.println("原始数据的长度: " + data.getBytes().length); ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout); //内容写到内存 gout.write(data.getBytes()); gout.close(); byte[] gzip = bout.toByteArray(); //从内存中取出压缩后的数据 System.out.println("压缩后的大小:" + gzip.length); response.setHeader("Content-Encoding", "gzip"); response.setHeader("Content-Length", gzip.length + ""); response.getOutputStream().write(gzip); } /* * 测试location头, 重定向到另一个资源用在登录以后跳到主页 */ private void test1(HttpServletResponse response) { response.setStatus(302); response.setHeader("location", "/Test/1.html"); } }
range头实现断点续传功能:
a.txt是咱们要下载的资源
package com.http.cn; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class RangeDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { URL url = new URL("http://localhost:8080/Test/a.txt"); HttpURLConnection con= (HttpURLConnection) url.openConnection(); con.setRequestProperty("Range", "bytes=5-"); InputStream in = con.getInputStream(); int len; byte[] b = new byte[1024]; FileOutputStream out = new FileOutputStream("c:\\a.txt"); while ((len=in.read(b)) > 0) { out.write(b, 0, len); } } }