• JAVA函数库


    1. 文件相关

    1.1 判断目录是否存在

    public static boolean dictionaryExist(String path) {
            File file = new File(path);
            if (file.exists() && file.isDirectory()) {
                return true;
            } else {
                return false;
            }
        }
    View Code

    1.2 创建多级目录

    public static void dictionaryCreate(String path) {
            File file = new File(path);
            if (file.exists() && file.isDirectory()) {
    
            } else {
                file.mkdirs();
            }
        }
    View Code

    1.3 判断文件是否存在

        public static boolean fileExist(String fileName) {
            File file = new File(fileName);
            if (file.exists() && file.isFile()) {
                return true;
            } else {
                return false;
            }
        }
    View Code

    1.4 删除文件

        public static void deleteFile(String fileName) {
            File file = new File(fileName);
            if (file.exists() && file.isFile()) {
                file.delete();
            }
        }
    View Code

    2. Http相关

    2.1 执行POST

        public static String doPost(String uri, String json) throws Exception {
            return doPost(uri, json, null);
        }
    
        public static String doPost(String uri, Map<String, Object> params) throws Exception {
            return doPost(uri, params, null);
        }
    
        public static String doPost(String uri, String json, Map<String, String> headers) throws Exception {
            logger.debug("Execute Http Post ----------------------->>>>>>");
            logger.debug("uri:" + uri);
            logger.debug("json:" + json);
            logger.debug("headers:" + headers);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(uri);
            if (headers != null) {
                Set<String> headerKeys = headers.keySet();
                for (String headerName : headerKeys) {
                    httpPost.setHeader(headerName, (String) headers.get(headerName));
                }
            }
            if (json != null) {
                httpPost.setEntity(new StringEntity(json, CODE_TYPE));
            }
            CloseableHttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new Exception("Error To Do Post:[" + response.getStatusLine().getStatusCode() + "] " + response.getStatusLine().getReasonPhrase());
            }
            HttpEntity entity = response.getEntity();
            String responseBody = EntityUtils.toString(entity, CODE_TYPE);
            httpClient.close();
            logger.debug("responseBody:" + responseBody);
            logger.debug("Complete Http Post ----------------------->>>>>>");
            return responseBody;
        }
    
        public static String doPost(String uri, Map<String, Object> params, Map<String, String> headers) throws Exception {
            logger.debug("Execute Http Post ----------------------->>>>>>");
            logger.debug("uri:" + uri);
            logger.debug("params:" + params);
            logger.debug("headers:" + headers);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(uri);
            if (headers != null) {
                Set<String> headerKeys = headers.keySet();
                for (String headerName : headerKeys) {
                    httpPost.setHeader(headerName, (String) headers.get(headerName));
                }
            }
            if (params != null) {
                List<NameValuePair> paramsPost = new ArrayList<NameValuePair>();
                Set<String> paramKeys = params.keySet();
                for (String paramName : paramKeys) {
                    paramsPost.add(new BasicNameValuePair(paramName, params.get(paramName).toString()));
                }
                httpPost.setEntity(new UrlEncodedFormEntity(paramsPost, CODE_TYPE));
            }
            CloseableHttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new Exception("Error To Do Post:[" + response.getStatusLine().getStatusCode() + "] " + response.getStatusLine().getReasonPhrase());
            }
            HttpEntity entity = response.getEntity();
            String responseBody = EntityUtils.toString(entity, CODE_TYPE);
            httpClient.close();
            logger.debug("responseBody:" + responseBody);
            logger.debug("Complete Http Post ----------------------->>>>>>");
            return responseBody;
        }
    View Code

    2.2 执行GET

        public static String doGet(String uri, Map<String, Object> params, Map<String, String> headers) throws Exception {
            logger.debug("Execute Http Get ----------------------->>>>>>");
            logger.debug("uri:" + uri);
            logger.debug("params:" + params);
            logger.debug("headers:" + headers);
            if (params != null) {
                uri = uri + "?";
                Set<String> paramKeys = params.keySet();
                for (String paramName : paramKeys) {
                    uri = uri + paramName + "=" + params.get(paramName) + "&";
                }
                uri = uri + "guid=" + UUID.randomUUID().toString();
            }
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(uri);
            if (headers != null) {
                Set<String> headerKeys = headers.keySet();
                for (String headerName : headerKeys) {
                    httpGet.setHeader(headerName, (String) headers.get(headerName));
                }
            }
            CloseableHttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new Exception("Error To Do Post:[" + response.getStatusLine().getStatusCode() + "] " + response.getStatusLine().getReasonPhrase());
            }
            HttpEntity entity = response.getEntity();
            String responseBody = EntityUtils.toString(entity, CODE_TYPE);
            httpClient.close();
            logger.debug("responseBody:" + responseBody);
            logger.debug("Complete Http Post ----------------------->>>>>>");
            return responseBody;
        }
    View Code

    3. String相关

    3.1 判断字符串为空

        public static final boolean isEmpty(String value) {
            return value == null || value.equals("");
        }
        
        public static final boolean isEmpty(Object value) {
            return value == null || value.toString().equals("");
        }
    View Code

    3.2 生成UUID

        public static final String getGUID() {
            return UUID.randomUUID().toString().replace("-", "");
        }
    View Code

    3.3 首字母大写

        public static final String firstCharUpper(String value) {
            return value.substring(0, 1).toUpperCase()+value.substring(1, value.length());
        }
    View Code

    3.4 给bean设定id

        public static final <T> T setNewID(Class<T> clazz, T object, String idField) throws Exception {
            idField = firstCharUpper(idField);
            Method getMethod = clazz.getMethod("get"+idField);
            Method setMethod = clazz.getMethod("set"+idField, String.class);
            if (getMethod == null || setMethod == null) {
                return object;
            } else {
                String id = (String) getMethod.invoke(object);
                if (isEmpty(id)) {
                    setMethod.invoke(object, getGUID());
                }
                return object;
            }
        }
    View Code

    4. 加解密相关

    4.1 MD5加密

    public final static String MD5(String content) throws UnsupportedEncodingException, NoSuchAlgorithmException {
            //用于加密的字符
            char md5String[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
            //使用平台的默认字符集将此 String 编码为 byte序列,并将结果存储到一个新的 byte数组中
            byte[] btInput = content.getBytes("UTF-8");
            //信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度的哈希值。
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            //MessageDigest对象通过使用 update方法处理数据, 使用指定的byte数组更新摘要
            mdInst.update(btInput);
            // 摘要更新之后,通过调用digest()执行哈希计算,获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {   //  i = 0
                byte byte0 = md[i];  //95
                str[k++] = md5String[byte0 >>> 4 & 0xf];    //    5
                str[k++] = md5String[byte0 & 0xf];   //   F
            }
            //返回经过加密后的字符串
            return new String(str);
        }
    View Code
  • 相关阅读:
    用gethub下载ardupilot的最新源码
    Mavlink协议理解
    Mavlink消息包解析
    Mavlink 协议 理解
    极飞P20农业无人机多机协同作业飞行
    如何下载最新的固件到Pixhawk
    韦东山笔记之用busybox构建根文件系统
    Visual studio 打包程序时添加自定义的系统必备组件
    MFC 编译链接错误:unresolved external symbol
    在Visual studio 2017中使用EF6连接MySQL
  • 原文地址:https://www.cnblogs.com/LOVE0612/p/5900477.html
Copyright © 2020-2023  润新知