HTTP协议(Hypertext transfer protocol),是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的过程。
HTTP请求:
HTTP响应:
302:资源移动到新地址,重定向。
304,307:要求客户机自行取缓存中的数据。
404:请求错误,服务器没有资源。
403:服务器有此资源,但客户机无权限。
实现数据压缩,gzip
String data = "aaaaaaaaa";
System.out.println("原始大小:"+data.getBytes().length);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(bout);
gout.write(data.getBytes());
gout.close();//如果数据量很小的时候,包状流有缓冲,不满就不会把数据传给数据流,close一下强行把数据给数据流。
byte gzip[] = bout.toByteArray();
System.out.println("压缩后大小:"+gzip.length());
//通知浏览器压缩格式。
response.setHeader("Content-Enconding","gzip");
response.setHeader("Content-Length",gzip.length());
设置响应格式
response.setHeader("content-type", "image/jpeg");
下载web资源到本地:
public class download {
public static void main(String[] args)throws Exception{
URL url = new URL("http://localhost:8080/Test/rangdownload.txt");
HttpURLConnection hc = (HttpURLConnection)url.openConnection();
hc.setRequestProperty("Rang", "bytes=5-");
InputStream in = hc.getInputStream();
int length = 0;
byte[] buffer = new byte[1024];
FileOutputStream out = new FileOutputStream("f:\\a.txt",true);
while ((length=in.read(buffer))>0){
out.write(buffer,0,length);
}
in.close();
out.close();
}
}