安卓基础_8

来源:互联网 发布:sql查询繁体转简体 编辑:程序博客网 时间:2024/05/05 13:38

1.range头介绍

Range 头代表请求的范围 多线程断点续传逻辑

1.
2.
3. //[1]创建URL对象
4. try {
5. URL url = new URL(“http://localhost:8080/day08/info.txt“);
6. //[2]获取httpurlconnection
7. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
8.
9. //[2.1]设置一个range头 告诉服务器从哪个位置开始下
10. conn.setRequestProperty(“Range”, “bytes=100-“);
11.
12. //[3]获取服务器返回的数据
13. InputStream in = conn.getInputStream(); //流里面的数据就是info.txt的数据
14.
15.
16. FileOutputStream fos = new FileOutputStream(“download.txt”);
17. int len = 0;
18. byte buffer[] = new byte[1024];
19. while((len=in.read(buffer))!=-1){
20. fos.write(buffer, 0, len);
21. }
22. fos.close();
23. in.close();
24. } catch (Exception e) {
25. e.printStackTrace();
26. }
27.
28.

2.servlet入门

[1]是用于开发动态网站的技术 属于javaee规范 [2]想查看servlet相关的技术 不能查看jdk文档 [3]servlet是一个接口 不能直接new 找默认实现类[4]servlet 是运行在 Web 服务器中的小型 Java 程序。servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web 客户端的请求。 [5]servlet程序要运行在tomcat中  servlet 用来处理请求和响应   实现步骤[1]定义一个类继承httpServlet[2]重写2个方法 一个是doGet方法和doPost方法 分别用来处理 get请求和post请求 

1.public class HelloWorldServlet extends HttpServlet {
2.
3.
4. //处理get请求
5. @Override
6. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
7. System.out.println(“get请求”);
8. super.doGet(req, resp);
9. }
10.
11.
12. //处理post请求
13. @Override
14. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15. // TODO Auto-generated method stub
16. super.doPost(req, resp);
17. }
18.
19.
20.}

[3]在web.xml中配置一下 原理就和Android清单文件一样 第一次先执行mainActivity

  1. helloworldServlet
  2. com.itheima.day08.HelloWorldServlet
  3. helloworldServlet
  4. /hello

3.servlet的执行过程

4.servlet的生命周期

人对比:出生 婴儿 儿童 小学 初中 高中 大学 黑马 结婚 生小孩 —->几十年后去另外世界

sevlet生命周期[1]当tomcat启动的时候 Servlet程序并没有初始化 [2]当我第一次访问该Servlet的时候会执行构造方法 init方法 和Service方法[3]当我第二次访问该Servlet只执行service方法 [4]service方法和doGet & doPost方法的关系  当是get请求会自动调用doGet方法 当进行post请求会调用doPost方法 所以我们在实际开发中不需要重写service直接重写doGet方法和doPost方法即可 [5]当tomcat正常关闭的情况下 Servlet会销毁  

5.urlpattern的三种写法

[1]完全路径匹配 /hello http://localhost:8080/day08/hello

[2]扩展名 .action .do

[3]目录匹配 /*

优先级:完全路径>目录匹配>扩展名

6.相对路径和绝对路径

打印乘法表  逻辑如下:

1.//[1]获取字符流 像客户端输出数据
2. PrintWriter writer = response.getWriter();
3.
4. //[2]获取客户端提交的数据
5. String name = request.getParameter(“name”);
6. int number = Integer.parseInt(name);
7. //[3]打印乘法表
8. for (int i = 1; i <= number; i++) {
9. for (int j = 1; j <=i; j++) {
10. writer.write(j+”*”+i+”=”+j*i+” “);
11. }
12. writer.println();
13. }
14.

相对路径:相对当前访问的页面路径而言 http://localhost:8080/day08/chengfabiao.html ChengFaBiaoServlet http://localhost:8080/day08/ChengFaBiaoServlet

http://localhost:8080/day08/ChengFaBiaoServlet

实际开发中使用绝对路径

绝对路径的写法:/ + 当前工程的项目名字 + Servlet的映射(就是web.xml中你配置的urlpattern的值)

相对路径应用场景 css中使用

7.servletConfig对象

servlet 容器使用的 servlet 配置对象,该对象在初始化期间将信息传递给 servlet。

ServletConfig对象是在Servlet初始化的时候就已经创建了

[1]获取servlet的名字

String servletName = getServletConfig().getServletName();

[2]获取配置信息

String name = getServletConfig().getInitParameter(“name”);

[3]获取servletContext对象

8.servletContext对象

理解 代表当前web应用

  1. //[1]通过servletConfig对象去获取servletContext对象
    2.// ServletContext servletContext = getServletConfig().getServletContext();
  2. //[2]第二种写法 获取servletcontext对象 代表当前web应用 代表的一个全局的信息
  3. ServletContext servletContext = getServletContext();
  4. String name = servletContext.getInitParameter(“name”);
  5. System.out.println(“name:”+name);

    1. //[3]类似Android中的 sp 对外共享数据
  6. servletContext.setAttribute(“itcast”, “传智播客最牛”);

    1. //[4]获取项目的真实路径 类似Android下的Environent String path = “/mnt/sdcard/info.txt”
  7. String fileUploadPath = servletContext.getRealPath(“upload”);

    总结servletContext有三个作用

    [1]获取全局的配置信息

    [2]对外共享数据 类似 Android下的sp

    [3]获取发布项目后的真实路径 域对象 容器

9.使用request域获取表单的数据

[1]下面这2个对象是tomcat服务器帮助我们初始化的

HttpServletRequest request, 代表请求

HttpServletResponse response 代表响应

[2] 获取请求的信息 代码如下

1.//[1]获取请求的方式
2. String method = request.getMethod();
3. System.out.println(“请求method:”+method);
4.
5.
6. String requestURI = request.getRequestURI();
7. System.out.println(“requestURI:”+requestURI);
8.
9.
10. //[2]获取请求头的信息
11. String hostHeader = request.getHeader(“Host”);
12. System.out.println(“hostHeader:”+hostHeader);
13.
14. //[3]获取表单提交的内容 name:的值就是表单input里面name属性的值
15.// String name = request.getParameter(“name”);
16. String[] parameterValues = request.getParameterValues(“fruit”);
17. for (String p : parameterValues) {
18. System.out.println(“name:”+p);
19. }
20. String pwd = request.getParameter(“pwd”);
21. System.out.println(“pwd:”+pwd);
22.

response:代表响应

像客户端输出信息

10 response解决乱码

 [1]字节流解决乱码 

实现代码:

1.response.setHeader(“Content-Type”, “text/html;charset=utf-8”);
2.
3. //[1]获取字节流 通过response对象
4.// OutputStream outputStream = response.getOutputStream();
5.// outputStream.write(data.getBytes(“utf-8”));

[2]字符流乱码的解决

1.//修改response域默认码表
2. response.setCharacterEncoding(“utf-8”);
3. //通知浏览器以utf-8查看
4. response.setHeader(“Content-Type”, “text/html;charset=utf-8”);
5. //[2]使用字符流像客户端输出数据
6. PrintWriter writer = response.getWriter();
7. writer.write(data);

11 request解决乱码

  post解决乱码  request.setCharacterEncoding("utf-8");  get解决乱码 

1.String name = request.getParameter(“name”);
2.byte[] bytes = name.getBytes(“iso-8859-1”);
3.String name1 = new String(bytes, “utf-8”);

url编码:  

1.String encode = URLEncoder.encode(data, “utf-8”);
2.
3.System.out.println(“encode:”+encode);*/
4.
5.String data = “%E5%95%8A%E4%BD%BC”;
6.String decode = URLDecoder.decode(data, “utf-8”);
7.System.out.println(“decode:”+decode);

12 文件上传

 文件上传应用场景: 朋友圈 百度云盘  360云  QQ邮箱上传附件 [4]文件上传原理 就是把客户端的文件拷贝到服务器上 客户端要求:  [1]要求必须有表单输入项 (必须有文件上传项)  [2]提交的方式必须是post  由于get大小有限制  [3]设置表单提交数据的类型 enctype,一般情况下不需要设置,只在上传时候需要设置    enctype="multipart/form-data"  [4]上传请求内容 如下

1.POST /_fileupload/ HTTP/1.1
2.Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, /
3.Referer: http://localhost:8080/_fileupload/
4.Accept-Language: zh-CN
5.User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)
6.Content-Type: multipart/form-data; boundary=—————————7e0375f590b32
7.Accept-Encoding: gzip, deflate
8.Host: localhost:8080
9.Content-Length: 495
10.Connection: Keep-Alive
11.Cache-Control: no-cache
12.
13.—————————–7e0375f590b32
14.Content-Disposition: form-data; name=”name”
15.
16.1111
17.—————————–7e0375f590b32
18.Content-Disposition: form-data; name=”file”; filename=”C:\Users\jinxing\Desktop\aa.txt”
19.Content-Type: text/plain
20.
21.gsgsggsgsgssgsgsgsggsgsgsggsgsgssgsgsgsggsgsgsggsgsgssgsgsgsggsgsgsggsgsgssgsgsgsggsgsgsggsgsgssgsgsgsggsgsgsggsgsgssgsgsgsggsgsgsggsgsgssgsgsgsggsgsgsggsgsgssgsgsgsggsgsgsggsgsgssgsgsgsggs
22.—————————–7e0375f590b32–

使用谷歌抓包

1.——WebKitFormBoundarya4CAWkMWXF2BQXAK
2.Content-Disposition: form-data; name=”name”
3.
4.xiaoli
5.——WebKitFormBoundarya4CAWkMWXF2BQXAK
6.Content-Disposition: form-data; name=”file”; filename=”aa.txt”
7.Content-Type: text/plain

  服务器端要求 实现有以下几种方式 (1)jspSmartUpload= 适于嵌入执行上传下载操作的JSP文件中,模型一  (2)fileUpload(模型二,mvc)FileUpload 是 Apache commons下面的一个子项目,组件FileUpload依赖于Commons IO组件   (3)servlet3.0版本实现了文件上传.     实现代码如下

1.//[1]获取普通上传项的内容
2. String name = request.getParameter(“name”);
3. System.out.println(“name:”+name);
4. //[2]获取文件上传的真实路径
5. String fileUploadpath = getServletContext().getRealPath(“upload”);
6. //[3]获取文件上传项的内容
7. Part part = request.getPart(“file”);
8.
9. //[3.1]对上传的文件进行 文件名字的截取 form-data; name=”file”; filename=”aa.txt”
10. String header = part.getHeader(“Content-Disposition”);
11. String[] split = header.split(“;”);
12. String[] split2 = split[2].split(“=”);
13. String fileName = split2[1].substring(1, split2[1].length()-1);
14.
15. System.out.println(“文件上传路径:”+fileUploadpath+”/”+fileName);
16.
17. //[4]把part里面的数据写到fileUploadpath目录下就可以了
18. part.write(fileUploadpath+”/”+fileName);

13 今日总结

  range 头      掌握   会单独创建sevlet doGet 方法和 doPost方法  doGet处理get请求  doPost处理 post请求   相对路径和绝对路径  了解   ServletConfig  了解      ServletContext对象  了解 能看懂我写的代码'  通过request 获取表单提交的数据   掌握    乱码的解决   了解   文件上传项   把基本的要求掌握 就可以了 
0 0
原创粉丝点击