小工具

json转换

    
        com.fasterxml.jackson.core
        jackson-databind
    
/**
 * Json工具类
 * base on jackson
 */
public class JsonUtil {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);

    /**
     * Object转json字符串
     *
     * @param object object
     * @return String
     */
    public static String toJsonString(Object object) {
        String json = null;
        if (object instanceof String) {
            json = (String) object;
        } else {
            try {
                json = MAPPER.writeValueAsString(object);
            } catch (JsonProcessingException e) {
                LOGGER.error("JsonUtil.toJsonString JsonProcessingException");
            }
        }
        return json;
    }

    /**
     * json格式字符串转Bean
     *
     * @param json  json
     * @param clazz Class.Type
     * @param    T
     * @return T
     */
    public static  T toBean(String json, Class clazz) {
        T t = null;
        try {
            t = MAPPER.readValue(json, clazz);
        } catch (IOException e) {
            LOGGER.error("JsonUtil.toBean IOException");
        }
        return t;
    }
}

http请求

    
        org.apache.httpcomponents
        httpclient
    
/**
 * http请求工具
 */
public class HttpExecuteUtil {

    public enum MIME {


        /**
         * 入参类型-json
         */
        JSON("application/json;charset=UTF-8"),

        /**
         * 入参类型-纯文本
         */
        PLAIN_TEXT("text/plain");

        /**
         * 入参类型
         */
        private String contentType;

        MIME(String contentType) {
            this.contentType = contentType;
        }

        public String getContentType() {
            return contentType;
        }
    }

    private static HttpClientBuilder httpClientBuilder;

    static {
        httpClientBuilder = HttpClients.custom();
    }

    private static HttpClient getHttpClient() {
        return httpClientBuilder.build();
    }

    /**
     * 无参形式进行GET访问
     *
     * @param url
     * @return
     * @throws Exception
     */
    public static String doGet(String url) throws Exception {
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Content-Type", MIME.JSON.getContentType());
        return EntityUtils.toString(getHttpClient().execute(httpGet).getEntity());
    }

    /**
     * 以Base64编码的形式进行GET访问
     *
     * @param url
     * @param param
     * @return
     * @throws Exception
     */
    public static String doGet(String url, String param) throws Exception {
        param = Base64.encodeBase64String(param.getBytes("UTF-8"));
        HttpGet httpGet = new HttpGet(url.concat("?").concat(param));
        httpGet.setHeader("Content-Type", MIME.PLAIN_TEXT.getContentType());
        String response = EntityUtils.toString(getHttpClient().execute(httpGet).getEntity());
        if (null != response) {
            response = new String(Base64.decodeBase64(response.getBytes("UTF-8")));
        }
        return response;
    }

    /**
     * 以参数形式进行GET访问
     *
     * @param url
     * @param paramMap
     * @return
     * @throws Exception
     */
    public static String doGet(String url, Map paramMap) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);
        for (Map.Entry entry : paramMap.entrySet()) {
            uriBuilder.addParameter(entry.getKey(), entry.getValue());
        }
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        httpGet.setHeader("Content-Type", MIME.JSON.getContentType());
        return EntityUtils.toString(getHttpClient().execute(httpGet).getEntity());
    }

    /**
     * 以参数形式进行POST访问
     *
     * @param url
     * @param paramMap
     * @return
     * @throws Exception
     */
    public static String doPost(String url, Map paramMap) throws Exception {
        HttpPost httpPost = new HttpPost(url);
        List params = new ArrayList<>();
        for (Map.Entry entry : paramMap.entrySet()) {
            params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        return EntityUtils.toString(getHttpClient().execute(httpPost).getEntity());
    }

    /**
     * 以JSON参数进行POST访问
     *
     * @param url
     * @param param
     * @param enableCode 是否使用Base64编码
     * @return
     * @throws Exception
     */
    public static String doPost(String url, String param, boolean enableCode) throws Exception {
        String contentType = MIME.JSON.getContentType();
        if (enableCode) {
            contentType = MIME.PLAIN_TEXT.getContentType();
            param = Base64.encodeBase64String(param.getBytes("UTF-8"));
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", contentType);
        StringEntity entity = new StringEntity(param, "UTF-8");
        httpPost.setEntity(entity);
        String response = EntityUtils.toString(getHttpClient().execute(httpPost).getEntity());
        return enableCode ? new String(Base64.decodeBase64(response.getBytes("UTF-8"))) : response;
    }

    /**
     * 以JSON参数进行POST访问
     *
     * @param url
     * @param param
     * @return
     * @throws Exception
     */
    public static String doPost(String url, String param) throws Exception {
        return doPost(url, param, Boolean.FALSE);
    }

}

读excel

   
        org.apache.poi
        poi
    
    
        org.apache.poi
        poi-ooxml
    
    
        org.apache.poi
        poi-ooxml-schemas
    
/**
 * 读Excel工具
 */
public class ExcelUtil {

    /**
     * 打开工作部
     *
     * @param filePath
     * @return
     * @throws IOException
     */
    private static Workbook getWorkBook(String filePath) throws IOException {
        InputStream inputStream = new FileInputStream(filePath);
        Workbook workbook = null;
        if (filePath.endsWith("xlsx")) {
            workbook = new XSSFWorkbook(inputStream);
        } else if (filePath.endsWith("xls")) {
            workbook = new HSSFWorkbook(inputStream);
        }
        return workbook;
    }

    /**
     * 读取数值和文本值
     *
     * @param cell
     * @return
     */
    private static String getCellValue(Cell cell) {
        String value = "";
        if (cell != null) {
            switch (cell.getCellTypeEnum()) {
                case NUMERIC:
                    value = new DecimalFormat("#").format(cell.getNumericCellValue());
                    break;
                case STRING:
                    value = cell.getStringCellValue().trim();
                    break;
            }
        }
        return value;
    }

    /**
     * 读取Excel
     *
     * @param filePath
     * @return 行号-列名-值
     * @throws IOException
     */
    public static Map> readFromExcel(String filePath) throws IOException {
        Map> data = new HashMap<>();
        Map columnMap = new HashMap<>();
        Workbook workBook = getWorkBook(filePath);
        for (Sheet sheet : workBook) {
            int firstRowNum = sheet.getFirstRowNum();
            int lastRowNum = sheet.getLastRowNum();
            Row firstRow = sheet.getRow(firstRowNum);
            for (Cell cell : firstRow) {
                columnMap.put(cell.getColumnIndex(), cell.getStringCellValue().trim());
            }

            for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
                Row row = sheet.getRow(rowNum);
                Map rowData = new HashMap<>();
                for (Map.Entry entry : columnMap.entrySet()) {
                    rowData.put(entry.getValue(), getCellValue(row.getCell(entry.getKey())));
                }
                data.put(rowNum, rowData);
            }
        }
        return data;
    }
}

你可能感兴趣的:(小工具)