如果请求里有这个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 200 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); } }