赞
踩
- package com.cmcc.flow.common.util;
-
- import java.io.UnsupportedEncodingException;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- import javax.servlet.http.HttpServletRequest;
-
- public class CharacterUtil {
- /**
- * 手机电话号码正则表达式
- */
- private final static String MOBILE_PHONE_NUM_REGEXP="^1[3-8][0-9]{9}$";
-
- private final static String EMAIL_ADDR_REGEXP="^\\w+((-\\w+)|(\\.\\w+))*@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$" ;
-
- public static String stringTranscoding(String str){
- if(str!=null && str.trim().length()>0 && str.trim()!=""){ //TODO:chendw 重复判断
- try {
- return new String(str.toString().getBytes("iso-8859-1"),"UTF-8");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- // e.printStackTrace();
- return null;
- }
- }
- return str;
- }
-
- /**
- * @throws UnsupportedEncodingException
- *
- * @Title: getStringFromRequest
- * @Description: 得到请求参数
- * @param @param request
- * @param @param parameterName
- * @param @return
- * @return String
- * @throws
- */
- public static String getStringFromRequest(HttpServletRequest request,
- String parameterName){
- try {
- request.setCharacterEncoding("utf-8");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(request==null || parameterName==null)
- return null;
-
- String szName = request.getParameter(parameterName);
- // if(szName!=null &&
- // request.getMethod().equalsIgnoreCase("get")){
- // try {
- String szRequestCharset = request.getCharacterEncoding();
- // szName = new String(szName.getBytes("ISO-8859-1"), "UTF-8");
- // } catch (UnsupportedEncodingException e) {
- // // TODO Auto-generated catch block
- // //e.printStackTrace();
- // return null;
- // }
- // }
-
- szName = trimStringSafe(szName);
- return szName;
- }
-
- /**
- *
- * @Title: getStringFromRequest
- * @Description: 从页面请求获取相应的参数,如果参数为空或者NULL,统一返回空字符
- * @param @param request
- * @param @param parameterName
- * @param @return
- * @return String
- * @throws
- *//*
- public static String getStringFromRequest(HttpServletRequest request,
- String parameterName)
- {
- try {
- return getStringFromRequest(request, parameterName, null);
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- return "";
- }
- }*/
-
- public static String trimStringSafe(String oriString)
- {
- if(oriString!=null)
- return oriString.trim();
- return null;
- }
-
- public static String addQueryString(Map<String, String> queryParams, String oriString)
- {
- if(oriString==null || oriString.equals("")){
- return "";
- }
-
- oriString+="?";
- for(String szKey : queryParams.keySet()){
- String szTmpString = szKey + "=" + queryParams.get(szKey);
- oriString+=szTmpString;
- oriString+="&";
- }
-
- int nLength = oriString.length();
- return oriString.substring(0, nLength-1);
- }
-
- /**
- *
- * @Title: isNullOrEmpty
- * @Description: 判断字符串是否为空值或空指针,如果是,抛出异常,并设置相应的异常信息
- * @param @param str
- * @param @param errorMsg
- * @param @throws Exception
- * @return void
- * @throws
- */
- public static void isNullOrEmpty(String str, String errorMsg)
- throws Exception
- {
- if(str==null || str.isEmpty()){
- Exception exception = new Exception(errorMsg);
- throw exception;
- }
- }
-
- /**
- *
- * @Title:isValidMobilePhone
- * @Description: 判断一个字符串是否为有效的手机号码
- * @param szMobilePhone
- * @return
- * @throws
- */
- public static boolean isValidMobilePhone(String szMobilePhone){
- return isValid(MOBILE_PHONE_NUM_REGEXP, szMobilePhone);
- }
-
- /**
- *
- * @Title:isValidEmailAddr
- * @Description: 判断一个字符串是否为有效的邮箱地址
- * @param szEmailAddr
- * @return
- * @throws
- */
- public static boolean isValidEmailAddr(String szEmailAddr){
- return isValid(EMAIL_ADDR_REGEXP, szEmailAddr);
- }
-
- /**
- *
- * @Title:isValid
- * @Description: 通用的验证方法
- * @param regExp
- * @param szValue
- * @return
- * @throws
- */
- public static boolean isValid(String regExp, String szValue){
- Pattern p = Pattern.compile(regExp);
- Matcher m = p.matcher(szValue);
- return m.find();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。