• java样例


    正则匹配

    // 正则匹配
    String line = "example.com/dynamic/infocheck";
    String pattern = "/dynamic/infocheck";
    Pattern r = Pattern.compile(pattern);
    // 方法一
    Matcher m = r.matcher(line);
    if (m.find( )) {
        System.out.println("Found value: " + m.group(0) );
    } else {
        System.out.println("NO MATCH");
    }
    // 方式二 正则前后需要加 .*
    boolean isMatch = Pattern.matches(pattern, line);
    System.out.println(isMatch);
    
    
    结果:
    Found value: /dynamic/infocheck
    false
    

    分割URL

    String target = "example.com/fjdlsfj/fjsadlfk/fdfjgg?a=1&b=2";
    int quote_index = target.indexOf("?");
    String url = "";
    String params = "";
    if(quote_index != -1){
        url = target.substring(0, quote_index);
        params = target.substring(quote_index+1);
    } else {
        url = target;
    }
    String host = "";
    String path = "";
    int slash_index = url.indexOf("/");
    if(slash_index != -1){
        host = url.substring(0, slash_index);
        path = url.substring(slash_index);
    } else {
        host = url;
    }
    System.out.println(url);
    System.out.println(host);
    System.out.println(path);
    System.out.println(params);
    
    
    结果:
    example.com/fjdlsfj/fjsadlfk/fdfjgg
    example.com
    /fjdlsfj/fjsadlfk/fdfjgg
    a=1&b=2
    

    split 分割取值

    String link = "11111	2222";
    System.out.println((link.split("	", 2))[0]);
    

    http获取json数据

    import com.fasterxml.jackson.databind.ObjectMapper;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.io.IOException;
    
    String name = "";
    MyHttpClient aa = new MyHttpClient();
    String request_result = aa.doGet("https://example.com/json_result");
    ObjectMapper mapper = new ObjectMapper();
    try {
        LinkedHashMap<String, Object> result_obj = mapper.readValue(request_result, LinkedHashMap.class);
        int code = (int) result_obj.get("code");
        if(code!=0){
            return result;
        }
        LinkedHashMap<String, String> other_data = (LinkedHashMap<String, String>) result_obj.get("data");
        name = other_data.get("name");
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    
    
    
    
    
    // MyHttpClient
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    
    class MyHttpClient {
        public static String doGet(String httpurl, String token) {
            HttpURLConnection connection = null;
            InputStream is = null;
            BufferedReader br = null;
            String result = null;// 返回结果字符串
            try {
                // 创建远程url连接对象
                URL url = new URL(httpurl);
                // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
    //            Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1", 8080));
    //            connection = (HttpURLConnection) url.openConnection(proxy);
                connection = (HttpURLConnection) url.openConnection();
                // 设置连接方式:get
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Accept", "application/json, text/plain, */*");
                if(token.length() != 0){
                    connection.setRequestProperty("Authorization", token);
                }
                // 设置连接主机服务器的超时时间:15000毫秒
                connection.setConnectTimeout(15000);
                // 设置读取远程返回的数据时间:60000毫秒
                connection.setReadTimeout(60000);
                // 发送请求
                connection.connect();
                // 通过connection连接,获取输入流
                if (connection.getResponseCode() == 200) {
                    is = connection.getInputStream();
                    // 封装输入流is,并指定字符集
                    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    // 存放数据
                    StringBuffer sbf = new StringBuffer();
                    String temp = null;
                    while ((temp = br.readLine()) != null) {
                        sbf.append(temp);
                        sbf.append("
    ");
                    }
                    result = sbf.toString();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭资源
                if (null != br) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != is) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                connection.disconnect();// 关闭远程连接
            }
            return result;
        }
    
    
        public String doGet(String httpurl) {
            return this.doGet(httpurl, "");
        }
    }
    
    
    
  • 相关阅读:
    Asp.net操作Excel----NPOI
    Python第一模块
    Sping笔记一览
    IO流技术一览
    事务技术一览
    日常问题记录
    分页与JDBC显示文档。
    分页技术与JDBC一览
    JDBC 技术开发
    MYSQL
  • 原文地址:https://www.cnblogs.com/huim/p/15160811.html
Copyright © 2020-2023  润新知