一個請求行、若干消息頭、以及實體內容,以下所示 :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); } } }