标签:
public static boolean dictionaryExist(String path) { File file = new File(path); if (file.exists() && file.isDirectory()) { return true; } else { return false; } }
public static void dictionaryCreate(String path) { File file = new File(path); if (file.exists() && file.isDirectory()) { } else { file.mkdirs(); } }
public static boolean fileExist(String fileName) { File file = new File(fileName); if (file.exists() && file.isFile()) { return true; } else { return false; } }
public static void deleteFile(String fileName) { File file = new File(fileName); if (file.exists() && file.isFile()) { file.delete(); } }
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; }
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; }
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(""); }
public static final String getGUID() { return UUID.randomUUID().toString().replace("-", ""); }
public static final String firstCharUpper(String value) { return value.substring(0, 1).toUpperCase()+value.substring(1, value.length()); }
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; } }
标签:
原文地址:http://www.cnblogs.com/LOVE0612/p/5900477.html