package com.ag.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.serializer.SerializerFeature; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * Created by shaoqiang on 8/28/16. */ public class CommonUtil { /** * 返回表格的beanlist * @param object * @param controlName * @param cudType * @param objeclass * @return */ public static List getBeanlist(JSONObject object, String controlName, String cudType, Class objeclass){ if (StrUtil.isEmpty(controlName)){ controlName = "datatable"; } String datarec1=((com.alibaba.fastjson.JSONObject) JSON.parseArray(object.getString(controlName)).get(0)).get(cudType).toString(); return JSON.parseArray(datarec1, objeclass); } /** * 获取grid中需强制更新的列名 * @param controlName * @return */ public static String[] getNeedColsByJson(JSONObject object, String controlName){ if (StrUtil.isEmpty(controlName)){ controlName = "datatable"; } String[] keys = null; JSONArray jsonArray = JSON.parseArray(object.getString(controlName)).getJSONObject(0).getJSONArray("update"); if (!jsonArray.isEmpty()){ com.alibaba.fastjson.JSONObject jsonObject = jsonArray.getJSONObject(0); Set keySet = jsonObject.keySet(); keySet.removeIf(key -> key.contains("#")); keys = new String[keySet.size()]; keySet.toArray(keys); } return keys; } public static String[] getNeedColsByJsonStr(String jsonStr){ com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(jsonStr); Set keySet = jsonObject.keySet(); keySet.removeIf(key -> key.contains("#")); String[] keys = new String[keySet.size()]; keySet.toArray(keys); return keys; } /** * 返回编辑表格的json数据 * @param object * @param dataparam 获取数据参数 * @param type 参数类型 包括create ,destory,update * @return */ public static String parseJson(JSONObject object,String dataparam,String type){ return ((com.alibaba.fastjson.JSONObject) JSON.parseArray(object.getString(dataparam)).get(0)).get(type).toString(); } public static String SendGET(String url,String param){ String result="";//访问返回结果 BufferedReader read=null;//读取访问结果 try { //创建url URL realurl=new URL(url+"?"+param); //打开连接 URLConnection connection=realurl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); //建立连接 connection.connect(); // 获取所有响应头字段 Map> map = connection.getHeaderFields(); // 遍历所有的响应头字段,获取到cookies等 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 read = new BufferedReader(new InputStreamReader( connection.getInputStream(),"UTF-8")); String line;//循环读取 while ((line = read.readLine()) != null) { result += line; } } catch (IOException e) { e.printStackTrace(); }finally{ if(read!=null){//关闭流 try { read.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } public static String time() { Date date=new Date(); DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time=format.format(date); return time; } /** * 字符串转化为日期格式 * @param date * @param format * @return */ public static Date getDate(String date,String format){ DateFormat ds=new SimpleDateFormat(format); Date date1=new Date(); try { date1= ds.parse(date); } catch (ParseException e) { e.printStackTrace(); } return date1; } /** * 获取时间函数 * @return */ public static Date getNowDate(){ Date nowDate=new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { nowDate=sdf.parse(sdf.format(new Date())); } catch (ParseException e) { e.printStackTrace(); } return nowDate; }; /** * 驼峰写法转表字段 * @param JsonString * @return */ public static String upJson(String JsonString) { if (JsonString == null) { return ""; } JSONArray jsonArray = JSONArray.parseArray(JsonString); List jsonList = new ArrayList(); for (int i = 0; jsonArray != null && i < jsonArray.size(); i++) { Map jsonMap = new HashMap(); com.alibaba.fastjson.JSONObject jsonObject = jsonArray.getJSONObject(i); if (jsonObject != null) { Set set = jsonObject.keySet(); for (String key : set) { jsonMap.put(underscoreName(key), jsonObject.get(key)); } jsonList.add(jsonMap); } } //关闭引用检测后,重复引用对象时就不会被$ref代替,但是在循环引用时也会导致StackOverflowError异常。 return JSON.toJSONString(jsonList, SerializerFeature.DisableCircularReferenceDetect); } /** * 处理字符 * @param name * @return */ public static String underscoreName(String name) { StringBuilder result = new StringBuilder(); if (name != null && name.length() > 0) { // 将第一个字符处理成大写 result.append(name.substring(0, 1).toUpperCase()); // 循环处理其余字符 for (int i = 1; i < name.length(); i++) { String s = name.substring(i, i + 1); // 在大写字母前添加下划线 if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) { result.append("_"); } // 其他字符直接转成大写 result.append(s.toUpperCase()); } } return result.toString(); } public static boolean existsField(Class clz,String fieldName){ try{ return clz.getDeclaredField(fieldName)!=null; } catch(Exception e){ //throw new RuntimeException(e.getCause()); } if(clz!=Object.class){ return existsField(clz.getSuperclass(),fieldName); } return false; } /** * 不够位数的在前面补0,保留code的长度位数字 * @param code * @return */ public static String autoGenericCode(String code) { String result = ""; // 保留code的位数 result = String.format("%0" + code.length() + "d", Integer.parseInt(code)); return result; } /** * 不够位数的在前面补0,保留num的长度位数字 * @param code * @return */ public static String autoGenericCode(String code, int num) { String result = ""; // 保留num的位数 // 0 代表前面补充0 // num 代表长度为4 // d 代表参数为正数型 result = String.format("%0" + num + "d", Integer.parseInt(code)); return result; } /** * 获取文件路径 * @return */ public static String getPath() { String str = CommonUtil.class.getResource("/").getPath(); String basicPath = str.substring(0, str.indexOf("target")); String path = basicPath + "target/QhdImtos/wb/document/"; return path; } public static void main(String args[]){ /* String port="9090"; String ports[]=port.split(","); System.out.println(ports[0]);*/ int a=2; int b=4; a=a^b^a; b=b^a^b; System.out.println(a+"==="+b); } }