http协议----->请求头和响应头





如果请求里有这个range头,那么响应里也有
1.首先在webroot下放好a.txt

内容如下:

2.然后在本地有个下载未完成的a.txt

本地a.txt内容如下:

DEMO:创建class
url:对应着eclipes里的a.txt
|
1
|
new FileOutputStream("/Users/snowing/Documents/a.txt",true);----》代表下载未完成的a.txt |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package cn.itcaste.web.servlet;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class RangeDemo { public static void main(String[] args) throws Exception { URL url=new URL("http://localhost:8082/day04/a.txt"); HttpURLConnection httpurlconnection=(HttpURLConnection) url.openConnection(); httpurlconnection.setRequestProperty("Range", "bytes=5-"); InputStream in=httpurlconnection.getInputStream(); int len=0; byte buffer[]=new byte[1024]; FileOutputStream out =new FileOutputStream("/Users/snowing/Documents/a.txt",true);//true:代表接着aaaaa继续下载而不是删除aaaaa再下载 while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } in.close(); out.close(); }} |
结果:


1.http相应格式

状态码:HTTP/1.1 OK
状态码

2.http状态行
200--》ok
302--》找别人要资源
304/307--》我不给你,你去缓存拿吧
403--》没权限
404-》地址错了,或者没资源
505-》服务器端问题
3.响应头
3.1.1location:这个头配合302使用,用于告诉客户找谁
用302和location进行重定向
创建servlet进行尝试
package cn.itcaste.web.servlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class servelt
*/
@WebServlet("/servelt")
public class servelt extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.getWriter().append("Served at: ").append(request.getContextPath());
response.setStatus(302);
response.setHeader("location","/day04/WebContent/1.html");
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }
3.1.2 servlet压缩传输数据
response.setHeader("Content-Encoding","gzip");
Content-Encoding--->通过这个头,浏览器知道数据压缩格式
response.setHeader("Content-Length", gzip.length+"");
Content-Length--->通过这个头,浏览器知道回送数据长度
package cn.itcaste.web.servlet; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class servelt
*/
@WebServlet("/servelt")
public class servelt extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.getWriter().append("Served at: ").append(request.getContextPath());
String data ="aaaaaaqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";
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.getOutputStream().write(gzip);//压缩数据打给浏览器
//通知浏览器数据采用压缩格式
response.setHeader("Content-Encoding","gzip");
response.setHeader("Content-Length", gzip.length+"");
response.getOutputStream().write(gzip); }
控制台:
原始数据大小:85
压缩后大小26
3.1.3 content-type--->通过这个头,浏览器知道回送数据格式
Tomcat 里的文件web.xml配置市面上常见的数据格式,不知道可以去这里找 距离:
</mime-mapping>
<mime-mapping>
<extension>jnlp</extension>
<mime-type>application/x-java-jnlp-file</mime-type>
</mime-mapping>
<mime-mapping>
<extension>joda</extension>
<mime-type>application/vnd.joost.joda-archive</mime-type>
</mime-mapping>
3.1.4 Last-Modified--->通过这个头,告诉浏览器当前资源缓存时间
3.1.5 Refresh:通过这个头告诉浏览器隔多长时间刷新一次
response.setHeader("Refresh", "3;url='http://www.sina.com'");//隔三秒刷新一下资源,跳到新浪官网
//response.setHeader("Refresh", "3;url='http://www.sina.com'");//每隔三秒刷新一下资源,适用于股票页面,聊天页面
String data= "aaaaa";
response.getOutputStream().write(data.getBytes());
3.1.6Content-Disposition--->通过这个头,告诉浏览器下载方式打开数据
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("content-disposition", "attachment;filename=3.jpg");//通过这个头,告诉浏览器下载方式打开数据
InputStream in=this.getServletContext().getResourceAsStream("/1.jpg");//通过这个头,浏览器知道回送数据格式
int len=0;
byte buffer[] = new byte[1024];
OutputStream out=response.getOutputStream();
while((len=in.read(buffer))>0){
out.write(buffer,0,len );
}
}
下载方式打开
整个DEMO
package cn.itcaste.web.servlet; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class servelt
*/
@WebServlet("/servlet")
public class servlet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.getWriter().append("Served at: ").append(request.getContextPath()); } private void test5(HttpServletResponse response) throws IOException {
response.setHeader("content-disposition", "attachment;filename=3.jpg");//通过这个头,告诉浏览器下载方式打开数据
InputStream in=this.getServletContext().getResourceAsStream("/1.jpg");//通过这个头,浏览器知道回送数据格式
int len=0;
byte buffer[] = new byte[1024];
OutputStream out=response.getOutputStream(); while((len=in.read(buffer))>0){
out.write(buffer,0,len );
}
} //刷新数据
private void test4(HttpServletResponse response) throws IOException {
response.setHeader("Refresh", "3;url='http://www.sina.com'");//隔三秒刷新一下资源,跳到新浪官网
//response.setHeader("Refresh", "3;url='http://www.sina.com'");//每隔三秒刷新一下资源,适用于股票页面,聊天页面
String data= "aaaaa";
response.getOutputStream().write(data.getBytes());
} //解析不同类型的数据
private void test3(HttpServletResponse response) throws IOException {
response.setHeader("content-type", "image/jpeg");//格式去web.xml里找
InputStream in=this.getServletContext().getResourceAsStream("/1.jpg");//通过这个头,浏览器知道回送数据格式
int len=0;
byte buffer[] = new byte[1024];
OutputStream out=response.getOutputStream(); while((len=in.read(buffer))>0){
out.write(buffer,0,len );
}
}
private void test2(HttpServletResponse response) throws IOException {
String data ="aaaaaaqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";
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.getOutputStream().write(gzip);//压缩数据打给浏览器
//通知浏览器数据采用压缩格式
response.setHeader("Content-Encoding","gzip");
response.setHeader("Content-Length", gzip.length+"");
response.getOutputStream().write(gzip);
} //用302和location进行重定向
public void test1(HttpServletResponse response){
response.setStatus(302);
response.setHeader("location","/day04/WebContent/1.html");
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }
http协议----->请求头和响应头的更多相关文章
- Android系列之网络(二)----HTTP请求头与响应头
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- (二)----HTTP请求头与响应头
一.HTTP头引入: 正确的设置HTTP头部信息有助于搜索引擎判断网页及提升网站访问速度.通常HTTP消息包括:客户机向服务器的请求消息和服务器向客户机的响应消 息.客户端向服务器发送一个请求,请求头 ...
- Android Http请求头与响应头的学习
本节引言: 上节中我们对Android涉及的网络编程进行了了解,也学习了下Http的基本概念,而本节我们 要学习的是Http的请求头与响应头,当然,可以把也可以把这节看作文档,用到的时候来查查 即可! ...
- HTTP请求头与响应头(转载)
欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/400503 ...
- http请求头和响应头详细解释
想对http请求头和响应头有更细致的了解,请看如下 Requests部分 Header 解释 示例 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/htm ...
- Http消息头中常用的请求头和响应头
作为Web开发对常用http的请求头和响应头熟悉了解一下还是很有必要的.比如请求头中Content-type指定了请求的内容,若类型是application/x-www-form-urlencoded ...
- HTTP请求头与响应头
http://m.blog.csdn.net/article/details?id=48918857 本篇文章中,将学习一下HTTP请求头与响应头的知识. 一.HTTP头引入: 正确的设置HTTP头部 ...
- http header详解,HTTP头、请求头、响应头、实体头
Content-Language,Content-Length,Content-Type,Content-Encoding,mime分析 Accept 指定客户端能够接收的内容类型 Accept:te ...
- HTTP请求的基本概念 HTTP请求头和响应头的含义
1,HTTP请求的基本概念 TCP/UPD/HTTP *2,HTTP请求头和响应头的含义 请求头: Accept: text/html,image/*(浏览器可以接收的类型) Acc ...
- [面试没答上的问题1]http请求,请求头和响应头都有什么信息?
最近在找工作,面试官问了一些问题自己并没有回答上,这里做一个小结. http请求,请求头和响应头都有什么信息? 页面和服务器交互最常见的方式就是ajax,ajax简单来说是浏览器发送请求到服务端,然后 ...
随机推荐
- shell脚本中执行mysql命令
1.mysql -hhostname -uuser -ppsword -e "mysql_cmd" 2. mysql -hhostname -uuser -ppsword < ...
- Recusively change the owner of files (chown) on Mac OS X
I've just changed my OS X / Mac main user - I've created a new 'coder' user account that I want to u ...
- 0059 Spring MVC与浏览器间的JSON数据转换--@RequestBody--@ResponseBody--MappingJacson2HttpMessageConverter
浏览器与服务器之间的数据交换有很多类型,不只是表单提交数据这一种,比如ajax技术就大量使用json.xml等,这时候就涉及到浏览器端和服务器端数据格式转换的问题,服务器端都是Java对象,需要把请求 ...
- bash之局部变量与子shell(转载)
shell是每个接触linux.unix用户不得不会的工具,谈到shell就又联系到bash,因为这个shell是普遍被使用的.那么bash中的局部变量和子shell你是否能熟练掌握呢?这里推荐一本学 ...
- ad7888 linux driver
/* ADCCONVERT.c : Generate ADC signals from SPI ports, as the A/D control signals. Author: Aaron Fu ...
- JVM与外界通过数据通道进行数据交换
使用I/O流访问file中的内容. JVM与外界通过数据通道进行数据交换. 分类: 按流分为输入流和输出流: 按传输单位分为字节流和字符流: 还可以分为节点流和过滤流. 节点流:负责数据源和程序之间建 ...
- 【BZOJ】1053: [HAOI2007]反素数ant(贪心+dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1053 约数个数等于分解出的质因数的(指数+1)的乘积这个就不用说了吧... 然后好神的题在于贪心.. ...
- Yii2.0实现微信公众号后台开发
接入微信 Yii2后台配置 1.在app/config/params.php中配置token参数 return [ //微信接入 'wechat' =>[ 'token' => 'your ...
- js 文件上传进度条
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...