package com.chinacreator.videoalliance.order.util; import java.io.UnsupportedEncodingException; import java.util.Date; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.DateUtils; import com.chinacreator.common.exception.BusinessException; import com.chinacreator.common.util.AESUtil; import com.chinacreator.common.util.URLUtil; public class ExternalUtil { private static final String USERMOB_REG = ".*(1\\d{10}).*"; private static final String BLACK_CHAR_REG = "[\\s-]"; /** * 获取json对象 * @param json * @param key * @param message * @return * @throws BusinessException */ public static String getValue(JSONObject json, String key, String message) throws BusinessException { if(json.containsKey(key)) { String value = json.getString(key); if("null".equals(value)) { value = null; } if(StringUtils.isEmpty(value) && message != null) { throw new BusinessException("8002",message + "参数无效"); } return value; } else if(message != null){ throw new BusinessException("8002", message + "参数无效"); } return null; } /** * 获取日期格式json对象 * @param json * @param key * @param message * @return * @throws Exception */ public static Date getDateValue(JSONObject json, String key, String message) throws Exception { String value = getValue(json, key, message); if(StringUtils.isNotEmpty(value)) { return DateUtils.parseDate(value, new String[]{"yyyyMMddHHmmss"}); } return null; } /** * 判断手机号码合法性 * @param usermob * @return */ public static boolean isValid(String usermob) { if(usermob == null) { return false; } usermob = usermob.replaceAll(BLACK_CHAR_REG, ""); return usermob.matches(USERMOB_REG); } /** * 格式发手机号码,过滤掉手机号码的多余的前缀和后缀 * @param usermob * @return */ public static String format(String usermob) { usermob = usermob.replaceAll(BLACK_CHAR_REG, ""); return usermob.replaceFirst(USERMOB_REG, "$1"); } public static void main(String[] args) { String data="{\"channelcode\":\"hlj\",\"order\":\"82EEA4B644555BFF465B272605953340A5342302043FCCAE8DCDCB3F2E8F44CE474375D92054AFCCFB925E197542E21601E49DBCE7BA27A837FA9C0E6DD5F99A\"}"; String data1="{\"productid\":\"8000610700\",\"userid\":\"18600000000\",\"imsi\":\"460030912121001\",\"expiredate\":\"20370101000000\",\"updatetype\":\"1\",\"effectivedate\":\"20140305010101\"}"; String data2="{\"userid\":\"CEE5E65A96BF9F1BD089B71EF8588808\",\"channelcode\":\"hlj\"}"; try { System.out.println(URLUtil.post("http://localhost:8080/videoalliance/orderex.do", data)); //System.out.println(AESUtil.encrypt("{\"productid\":\"8000610900\",\"userid\":\"18603663462\"}", "90b94c66ac614d0d")); } catch (Exception e) { e.printStackTrace(); } } }