zoukankan      html  css  js  c++  java
  • 读取文档并打印

    http.txt文档内容如下

    POST /login HTTP/1.1
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
    Referer: http://127.0.0.1:8088/1.html
    Accept-Language: zh-cn
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: 127.0.0.1:8088
    Content-Length: 19
    Connection: Keep-Alive
    Cache-Control: no-cache

    userName=qq&pwd=111&age=20&edu=大专

    要求:将第一行post封装到method属性、/login封装到url、HTTP/1.1封装到protocol属性。第二行到倒数第三行封装到headMap<String, String>,最后一行封装到paramMap<String, String>.


    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    public class Request {
      private String method;
      private String protocol;
      private String url;
      private Map<String, String> headMap = new HashMap<String, String>();
      private Map<String, String> paramMap = new HashMap<String, String>();
      public Request(){
        Reader r = null;
        BufferedReader br = null;
        List<String> list = new ArrayList<String>();
        try {
          r = new FileReader("http.txt");
          br = new BufferedReader(r);
          String str = "";
          while((str = br.readLine())!= null){
            list.add(str);
          }

        } catch (Exception e) {
          e.printStackTrace();
        }finally {
          try {
            br.close();
            r.close();
          } catch (IOException e) {
            e.printStackTrace();
          }

        }
        this.pressLine(list.get(0));
        this.pressHead(list);
        this.pressPara(list.get(list.size()-1));
      }

      private void pressLine(String line) {
        String[] array = line.split(" ");
        this.method = array[0];
        this.url = array[1];
        this.protocol = array[2];
      }
      private void pressHead(List<String> list) {
        for(int i = 1; i < list.size()-2; i++){
        String[] array1 = list.get(i).split(": ");
        this.headMap.put(array1[0], array1[1]);
        }
     }
      private void pressPara(String s) {
        String[] array2 = s.split("&");
        for(String a : array2){
          String[] str1 = a.split("=");
          this.paramMap.put(str1[0], str1[1]);
        }
      }

      public String getMethod() {
        return this.method;
      }
      public String getProtocol() {
        return this.protocol;
      }

      public String getUrl() {
        return this.url;
      }

      public String getHeadMap(String key) {
        return this.headMap.get(key);
      }

      public String getParamMap(String key) {
        return this.paramMap.get(key);
      }

      @Override
      public String toString() {
        return "Request [method=" + method + ", protocol=" + protocol + ", url=" + url + ", headMap=" + headMap
          + ", paramMap=" + paramMap + "]";
      }

     }

    class RequestTest {

      public static void main(String[] args) {
        Request r = new Request();
        String method = r.getMethod();
        String url = r.getUrl();
        String protocol = r.getProtocol();
        String headValue = r.getHeadMap("Host");
        String paraValue = r.getParamMap("userName");
        System.out.println(method+" "+url+" "+protocol+" "+paraValue+" "+headValue);
      }

    }

  • 相关阅读:
    elasticsearch-排序(六)
    elasticsearch-分词器(五)
    elasticsearch-搜索-基本搜索(四)
    elasticsearch-文档(三)
    elasticsearch-集群(二)
    FFmpeg架构之I/O模块分析
    DirectShow 在 VS2010 中开发环境的设置
    预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)
    2012年软件开发者薪资调查报告
    深入了解 VP8
  • 原文地址:https://www.cnblogs.com/tcc89/p/5402099.html
Copyright © 2011-2022 走看看