All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dahua.eco.base.spring.utils.ParamUtils Maven / Gradle / Ivy

package com.dahua.eco.base.spring.utils;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.*;

public class ParamUtils {
    /**
     * 

* Checks if a String is empty ("") or null. *

* *
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * 
* *

* NOTE: This method changed in Lang version 2.0. It no longer trims the * String. That functionality is available in isBlank(). *

* * @param str * the String to check, may be null * @return true if the String is empty or null */ public static boolean isEmpty(String str) { return str == null || str.length() == 0; } /** *

* Checks if a String is not empty ("") and not null. *

* *
     * StringUtils.isNotEmpty(null)      = false
     * StringUtils.isNotEmpty("")        = false
     * StringUtils.isNotEmpty(" ")       = true
     * StringUtils.isNotEmpty("bob")     = true
     * StringUtils.isNotEmpty("  bob  ") = true
     * 
* * @param str * the String to check, may be null * @return true if the String is not empty and not null */ public static boolean isNotEmpty(String str) { return !isEmpty(str); } /** *

* Checks if a String is whitespace, empty ("") or null. *

* *
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * 
* * @param str * the String to check, may be null * @return true if the String is null, empty or whitespace * @since 2.0 */ public static boolean isBlank(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) == false)) { return false; } } return true; } /** *

* Checks if a String is not empty (""), not null and not whitespace only. *

* *
     * StringUtils.isNotBlank(null)      = false
     * StringUtils.isNotBlank("")        = false
     * StringUtils.isNotBlank(" ")       = false
     * StringUtils.isNotBlank("bob")     = true
     * StringUtils.isNotBlank("  bob  ") = true
     * 
* * @param str * the String to check, may be null * @return true if the String is not empty and not null and * not whitespace * @since 2.0 */ public static boolean isNotBlank(String str) { return !isBlank(str); } /** *
  • 用於处理JS参解码,配合WEB页面二次转码
  • *
  • JS:params="&name="+encodeURI(encodeURI("姓名"));
  • *
  • JAVA:ParamUtil.decode(request,"name")=="姓名"
  • * * @param value * @return * @throws UnsupportedEncodingException */ public static String decode(String value) { try { return java.net.URLDecoder.decode(value, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } /** *
  • 获取Request中的参,并且去除NULL
  • * * @param request * @param param * @return */ public static String get(HttpServletRequest request, String param ){ return get(request, param,""); } /** *
  • 获取Request中的参,并且去除NULL,以指定内容取代
  • * * @param request * @param param * @param defValue * @return */ public static String get(HttpServletRequest request, String param, String defValue) { String value = request.getParameter(param); return null == value ? defValue : value.trim(); } /** * 整型值 * * @param request * @param param * @param defValue * @return */ public static int getInt(HttpServletRequest request, String param, int defValue) { String value = request.getParameter(param); if (!isEmpty(value)) { try { return Integer.parseInt(value); } catch (Exception e) { } } return defValue; } /** * 布林值 * * @param request * @param param * @return */ public static boolean getBoolean(HttpServletRequest request, String param) { return "true".equals(get(request, param)); } /** * 金额类型 * * @param request * @param param * @return */ public static Double getDouble(HttpServletRequest request, String param) { try { String value = get(request, param, "0").replaceAll(",", ""); return new Double(value); } catch (Exception e) { } return new Double(0.0);//zhangxunhong 于2012-4-27 修改 (旧版是 return null) } /** * 日期类型字串yyyyMMdd * * @param request * @param param * @return */ public static String getDateStr(HttpServletRequest request, String param) { return get(request, param).replaceAll("-|\\/", ""); } /** * 向从Request中获取字串值到Map中,包含''值(ibatis动态条件用到) * * @param request * @param param */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void putStr2Map(HttpServletRequest request, String param, Map paramMap) { String value = request.getParameter(param); if (null != value) { paramMap.put(param, value.trim()); } } /** * 向从Request中获取字串值到Map中,包含''值(ibatis动态条件用到) 去掉了左边的空格 2010-04-29 fanhong * * @param request * @param param */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void putQRYStr2Map(HttpServletRequest request, String param, Map paramMap) { String value = request.getParameter(param); if (null != value) { paramMap.put(param, lTrim(value)); } } /** * 向从Request中获取整型值到Map中,不包含空值 * * @param request * @param param */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void putInt2Map(HttpServletRequest request, String param, Map paramMap) { paramMap.put(param, new Integer(getInt(request, param, 0))); } /** * 向从Request中获取浮点值到Map中,不包含空值 * * @param request * @param param */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void putDouble2Map(HttpServletRequest request, String param, Map paramMap) { paramMap.put(param, getDouble(request, param)); } /** * 向从Request中将日期值yyyy-MM-dd转成yyyyMMdd到Map中,不包含空值 * * @param request * @param param * @param map */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void putDateStr2Map(HttpServletRequest request, String param, Map map) { String value = request.getParameter(param); if (null != value) { map.put(param, value.replaceAll("-|\\/", "")); } } private static String[] forbids = { "action" }; private static boolean checkParams(String name) { for (int i = 0, j = forbids.length; i < j; i++) { if (forbids[i].equals(name)) return false; } return true; } public static String generyHiddenInput(String name, String value) { String html = ""; html = html.replaceFirst("0", name); html = html.replaceFirst("1", value); return html; } public static String fixParamToHtml(String[] params, HttpServletRequest request, String prefix) { StringBuffer bf = new StringBuffer(); for (int i = 0, j = params.length; i < j; i++) { String name = params[i]; String value = get(request, name); if (!isEmpty(value)) { bf.append(generyHiddenInput(prefix + name, value)); } } return bf.toString(); } public static String fixParamToHtml(HttpServletRequest request, String prefix) { return fixParamToHtml(request, prefix, true); } @SuppressWarnings("rawtypes") public static String fixParamToHtml(HttpServletRequest request, String prefix, boolean forbid) { StringBuffer bf = new StringBuffer(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String value = get(request, name); if (!isEmpty(value)) { if (forbid) { if (!checkParams(name)) { continue; } } bf.append(generyHiddenInput(prefix + name, value)); } } return bf.toString(); } @SuppressWarnings("rawtypes") public static String decodeFixParamToHtml(HttpServletRequest request, String prefix) { StringBuffer bf = new StringBuffer(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (name.startsWith(prefix)) { try { String value = decode(get(request, name)); if (!isEmpty(value)) { bf.append(generyHiddenInput(name, value)); } } catch (Exception e) { } } } return bf.toString(); } @SuppressWarnings("rawtypes") public static String generyFixParamToHtml(HttpServletRequest request, String prefix) { StringBuffer bf = new StringBuffer(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (name.startsWith(prefix)) { try { String value = get(request, name); if (!isEmpty(value)) { bf.append(generyHiddenInput(name, value)); } } catch (Exception e) { } } } return bf.toString(); } @SuppressWarnings("rawtypes") public static String revertFixParamToHtml(HttpServletRequest request, String prefix) { StringBuffer bf = new StringBuffer(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (name.startsWith(prefix) && !"_backUrl".equals(name)) { String value = get(request, name); name = name.substring(prefix.length()); if (!isEmpty(value)) { bf.append(generyHiddenInput(name, value)); } } } return bf.toString(); } @SuppressWarnings("rawtypes") public static String deRevertFixParamToHtml(HttpServletRequest request, String prefix) { StringBuffer bf = new StringBuffer(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (name.startsWith(prefix)) { try { String value = decode(get(request, name)); if (!isEmpty(value)) { name = name.substring(prefix.length()); bf.append(generyHiddenInput(name, value)); } } catch (Exception e) { } } } return bf.toString(); } /** * 将装饰过的HttpServletRequest参分离出来,存储到MAP中 *
  • request: _name ==> map:_name
  • * * @param request * @param prefix * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Map enfixParam(HttpServletRequest request, String prefix) { Enumeration names = request.getParameterNames(); Map params = new HashMap(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (name.startsWith(prefix)) { String value = get(request, name); if (!isEmpty(value)) { params.put(name, value); } } } return params; } /** *
  • 还原被装饰过的HttpServletRequest参
  • *
  • 去除装饰,并将还原后的参存储到MAP中
  • *
  • request: _name ==> map:name
  • * * @param request * @param prefix * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Map defixParam(HttpServletRequest request, String prefix) { Enumeration names = request.getParameterNames(); Map params = new HashMap(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (name.startsWith(prefix)) { String value = get(request, name); name = name.substring(prefix.length()); if (!isEmpty(value)) { params.put(name, value); } } } return params; } /** * 将装饰过的HttpServletRequest参分离出来,并拼接成一个新的Url字串 * * @param request * @param prefix * @return a=b&c=d&... */ @SuppressWarnings("rawtypes") public static String getEnfixParamString(HttpServletRequest request, String prefix) { Enumeration names = request.getParameterNames(); StringBuffer uri = new StringBuffer(""); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (name.startsWith(prefix)) { String value = get(request, name); if (!isEmpty(value)) { uri.append("&"); uri.append(name); uri.append("="); uri.append(decode(value)); } } } return uri.toString(); } /** * 解析请求,并将参值对以key:value字串保存 * * @param request * @return */ @SuppressWarnings("rawtypes") public static String favorit(HttpServletRequest request) { String url = request.getRequestURL().toString(); String ctx = request.getContextPath(); int index = url.indexOf(ctx); String name, value; StringBuffer bf = new StringBuffer(); bf.append("URL:'").append(url.substring(index + ctx.length() + 1)) .append("'"); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { name = (String) names.nextElement(); value = get(request, name); if (!(null == value || "".equals(value))) { bf.append(",").append(name).append(":\"").append(value).append( "\""); } } return bf.toString(); } /** * 主机报表,大资料档案需要拆分,本方法可以在原档名中加入拆分档案序号, 获得拆分档名 * * @param fileName * @param xSizeIndex * @param fix * @return */ public static String splitFileName(String fileName, int xSizeIndex, String fix) { if (xSizeIndex > 0) { // abc.del To abc_N.del int index = fileName.lastIndexOf(fix); StringBuffer bf = new StringBuffer(); bf.append(fileName.substring(0, index)); bf.append("_").append(xSizeIndex); bf.append(fileName.substring(index)); return bf.toString(); } return fileName; } // 2010-04-29去掉左边的空格 public static String rTrim(String s) { int len = s.length(); int st = 0; int off = 0; /* avoid getfield opcode */ char[] val = s.toCharArray(); /* avoid getfield opcode */ while ((st < len) && (val[off + len - 1] <= ' ')) { len--; } return ((st > 0) || (len < s.length())) ? s.substring(st, len) : s; } // 2010-04-29去掉右边的空格 public static String lTrim(String s) { int len = s.length(); int st = 0; int off = 0; /* avoid getfield opcode */ char[] val = s.toCharArray(); /* avoid getfield opcode */ while ((st < len) && (val[off + st] <= ' ')) { st++; } return ((st > 0) || (len < s.length())) ? s.substring(st, len) : s; } // 拆分字符串,指定返回数组的字符串,下标从1开始 public static String ToOrderCol(HttpServletRequest request, String col, int n) { String orderCol = ParamUtils.get(request, col); if (n >= 0) { if (!"".equals(orderCol)) { String[] splitCol = orderCol.split(","); if (0 != splitCol.length) { if (n == 0) { return splitCol[n]; } else { return splitCol[n - 1]; } } } } return ""; } public static Map transBean2Map(Object obj) { if(obj == null){ return null; } Map map = new HashMap(); try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); // 过滤class属性 if (!key.equals("class")) { // 得到property对应的getter方法 Method getter = property.getReadMethod(); Object value = getter.invoke(obj); map.put(key, value); } } } catch (Exception e) { } return map; } /*把request参数转化为Map*/ @SuppressWarnings("unchecked") public static Map param2Map(HttpServletRequest request) { Map param = new HashMap(); Enumeration names = request.getParameterNames(); for (Enumeration e = names; e.hasMoreElements();) { String thisName = e.nextElement(); String thisValue = request.getParameter(thisName); param.put(thisName, thisValue.replaceAll("%26", "&").replaceAll("%23", "#").replaceAll("%25", "%").replaceAll("%", "/%").replaceAll("_", "/_").replaceAll("//_", "/_")); } return param; } /** * 把request参数转化为JSONObject */ @SuppressWarnings("unchecked") public static JSONObject param2JSONObject(HttpServletRequest request) { JSONObject jsonObject = new JSONObject(); Enumeration names = request.getParameterNames(); for (Enumeration e = names; e.hasMoreElements();) { String thisName = e.nextElement(); String thisValue = request.getParameter(thisName); jsonObject.put(thisName, thisValue); } return jsonObject; } public static boolean containsKey(List> list, String key, String value){ if(list != null && list.size() > 0){ for(int index=0; index < list.size(); index++){ Map item = list.get(index); if(value.equals(item.get(key))){ return true; } } } return false; } /** * 添加get请求参数 * @param url * @param params * @return */ public static String getUrl(String url, Map params){ //添加url参数 if ( params != null) { Iterator it=params.keySet().iterator(); StringBuffer sb=null; while(it.hasNext()){ String key=it.next(); Object value=params.get(key); if(value == null) { continue; } if(sb==null){ sb=new StringBuffer(); sb.append("?"); }else{ sb.append("&"); } sb.append(key); sb.append("="); sb.append(value); } url+=sb==null?"":sb.toString(); } return url; } /** * 追加参数信息到url后面,形成完整的请求url * * @param url 原url(不含?后面的参数信息) * @param args 需要附带的JSONObject类型参数 * * @return 完整的带有参数的请求url */ public static String getUrlWithParams(String url, JSONObject args) { if (args == null) { return url; } Map paramMap = new HashMap(); for (String key : args.keySet()) { Object valueObject = args.get(key); // 只接收简单的String/Integer类型的参数 if (valueObject instanceof String || valueObject instanceof Integer) { paramMap.put(key, String.valueOf(valueObject)); } } String resultUrl = ParamUtils.getUrl(url, paramMap); return resultUrl; } }




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy