| | |
| | | package cn.ksource.core.util; |
| | | |
| | | import org.apache.commons.lang.StringEscapeUtils; |
| | | import org.apache.commons.lang.StringUtils; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.Method; |
| | | import java.lang.reflect.Modifier; |
| | |
| | | import java.net.URLEncoder; |
| | | import java.util.*; |
| | | |
| | | import org.apache.commons.lang.StringEscapeUtils; |
| | | import org.apache.commons.lang.StringUtils; |
| | | |
| | | |
| | | public class ConvertUtil { |
| | | |
| | | |
| | | public static Boolean obj2Boolean(Object obj){ |
| | | return obj == null ? false : Boolean.getBoolean(obj.toString()); |
| | | } |
| | | |
| | | |
| | | public static Double obj2Double(Object obj){ |
| | | if (obj == null) { |
| | | return null; |
| | | } |
| | | return StringUtil.isBlank(obj2Str(obj)) ? null : Double.valueOf(obj.toString()); |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | String str = "0.123"; |
| | | System.out.println(obj2Double(str)); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * List转Map |
| | | * @param dataList |
| | |
| | | } |
| | | return resultMap; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * List转Map |
| | | * @param dataList |
| | |
| | | } |
| | | return resultMap; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 如果为空,使用提供的值替换 |
| | | * @param obj |
| | |
| | | public static Object defaultIfEmpty(Object obj,Object o){ |
| | | return obj == null || StringUtils.isBlank(obj.toString()) || StringUtils.equalsIgnoreCase(obj.toString(), "null") ? o : obj; |
| | | } |
| | | |
| | | |
| | | /**將GBK转化为GBK |
| | | * @param str |
| | | * @return |
| | |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | |
| | | public static String html2Text(String html){ |
| | | return html.replaceAll("<[^>]+>", "").replaceAll(" ",""); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 功能描述:Object类转换为String,避免在Object为null时,直接toString()出错<BR> |
| | | * @param obj |
| | |
| | | return obj == null ? "" : obj.toString(); |
| | | } |
| | | public static Integer obj2Integer(Object obj){ |
| | | return obj == null || obj.toString().trim().equals("") ? null : Integer.parseInt(obj.toString()); |
| | | return obj == null || obj.toString().trim().equals("") ? null : Integer.parseInt(obj.toString()); |
| | | } |
| | | |
| | | |
| | | public static int obj2Int(Object obj){ |
| | | if(null==obj) { |
| | | System.out.println("1121313"); |
| | | } |
| | | return obj == null || obj.toString().trim().equals("") ? null : Double.valueOf(obj.toString()).intValue(); |
| | | return obj == null || obj.toString().trim().equals("") ? 0 : Double.valueOf(obj.toString()).intValue(); |
| | | } |
| | | |
| | | |
| | | public static Long obj2Long(Object obj){ |
| | | return obj == null || obj.toString().trim().equals("") ? null : Long.parseLong(obj.toString()); |
| | | return obj == null || obj.toString().trim().equals("") ? null : Long.parseLong(obj.toString()); |
| | | } |
| | | |
| | | |
| | | public static Long obj2Long(Object obj,boolean filter){ |
| | | if (!filter) { |
| | | return obj == null || obj.toString().trim().equals("") ? null : Long.parseLong(obj.toString()); |
| | | return obj == null || obj.toString().trim().equals("") ? null : Long.parseLong(obj.toString()); |
| | | } else { |
| | | return obj == null || obj.toString().trim().equals("") ? null : Long.parseLong(obj.toString().replaceAll("[^0-9]", "")); |
| | | return obj == null || obj.toString().trim().equals("") ? null : Long.parseLong(obj.toString().replaceAll("[^0-9]", "")); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 功能描述:过滤用户输入的html、sql、javascript脚本<BR> |
| | | * @param map |
| | |
| | | } |
| | | return resultMap; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将byte转化为K或M,大于1024K的按M计算,返回*K或*M |
| | | * @param byteSize |
| | |
| | | BigDecimal b1 = new BigDecimal(byteSize); |
| | | BigDecimal b2 = new BigDecimal(1024); |
| | | double k = b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | |
| | | |
| | | //大于1024,转化为M |
| | | if (k > 1024) { |
| | | b2 = new BigDecimal(1024*1024); |
| | | k = b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | |
| | | |
| | | return k + "M"; |
| | | } |
| | | |
| | | |
| | | return k + "K"; |
| | | } |
| | | } |