package com.ourlinc.activity;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 提供通用的web处理方法
*
* @author lipeiying
*
*/
public class WebUtils {
public final static String ONLY_DAY = "yyyy-MM-dd";
public final static String HAVA_MINUTES = "yyyy-MM-dd HH:mm";
private WebUtils() {
}
/**
* 返回一个不为空的字符串
*
* @param str
* @return
*/
public static String toString(String str) {
return null == str ? "" : str;
}
/**
* 当s的长度等0或为空时返回真
*
* @param str
* @return
*/
public static boolean isEmptyString(String s) {
return null == s || 0 == s.length();
}
/**
* 把字符转化为整数,转化失败返回1
*
* @param s
* @return
*/
public static int toInt(String s) {
return toInt(s, 1);
}
public static int toInt(String s, int defaultValue) {
int i = defaultValue;
if (null != s) {
try {
i = Integer.parseInt(s);
} catch (NumberFormatException e) {
}
}
return i;
}
/**
* 把一个String对象转换成yyyy-MM-dd格式的Date对象
*
* @param strOneDate
* @return
*/
public final static Date strFormatDate(String strOneDate) {
return strFormatDate(strOneDate, ONLY_DAY);
}
/**
* 把一个String对象转换成yyyy-MM-dd HH:mm格式的Date对象
*
* @param strOneDate
* @return
*/
public final static Date strFormatDateAndTime(String strOneDate) {
return strFormatDate(strOneDate, HAVA_MINUTES);
}
/**
* 将String转化为指定格式的date对象
*
* @param strOneDate
* @param type
* date的格式,有yyyy-MM-dd格式,有yyyy-MM-dd HH:mm格式
* @return
*/
public final static Date strFormatDate(String strOneDate, String type) {
DateFormat format = new SimpleDateFormat(type);
Date date = new Date();
try {
date = format.parse(strOneDate);
} catch (ParseException e) {
}
return date;
}
/**
* 把一个Date对象转换成yyyy-MM-dd格式的字符串
*
* @param date
* @return 转换后的Date对象
*/
public final static String dateFormatToStr(Date date) {
DateFormat format = new SimpleDateFormat(ONLY_DAY);
return format.format(date);
}
/**
* 把一个Date对象转换成yyyy-MM-dd HH:mm格式的字符串
*
* @param date
* @return
*/
public final static String dateAndTimeFormatToStr(Date date) {
DateFormat format = new SimpleDateFormat(HAVA_MINUTES);
return format.format(date);
}
public final static Date getClearDate() {
String str = dateAndTimeFormatToStr(new Date());
return strFormatDateAndTime(str);
}
/**
* base64解密
*
* @param key
* @return
* @throws IOException
* @throws Exception
*/
public static byte[] decryptBASE64(String key) throws IOException {
return new BASE64Decoder().decodeBuffer(key);
}
/**
* base64加密
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* MD5加密
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptMD5(byte[] data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(data);
return md5.digest();
}
/**
* 用于将一个字符串按照一定的编码格式进行编码
*
* @param s
* 需要编码的字符串
* @param enc
* 需要转为什么字符集
* @return 返回转换后的字符串
* @throws UnsupportedEncodingException
*/
public static final String decode(String s, String enc)
throws UnsupportedEncodingException {
return isEmptyString(s) ? "" : URLDecoder.decode(s, enc);
}
public final static String MD5(String s) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] strTemp = s.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
public static final String encodeUrl(String str) {
if ((str == null) || (str.length() == 0))
return "";
return encodeUrl(str, null).toString();
}
public static final StringBuilder encodeUrl(String str, StringBuilder sb) {
if (str == null)
return ((sb == null) ? new StringBuilder(0) : sb);
if (sb == null)
sb = new StringBuilder(str.length() * 3);
int len = str.length();
for (int i = 0; i < len; ++i) {
char ch = str.charAt(i);
if ((ch < 0) || (ch > '')) {
int utf16 = (ch < 0) ? 65536 + ch : ch;
if ((utf16 >= 128) && (utf16 < 2047)) {
encodeUrl(0xC0 | 0x1F & utf16 >> 6, sb);
encodeUrl(0x80 | 0x3F & utf16, sb);
} else if (utf16 >= 2048) {
encodeUrl(0xE0 | 0xF & utf16 >> 12, sb);
encodeUrl(0x80 | 0x3F & utf16 >> 6, sb);
encodeUrl(0x80 | 0x3F & utf16, sb);
}
} else if (('.' == ch) || ('-' == ch) || ('_' == ch) || ('*' == ch)
|| ((ch >= '0') && (ch <= '9'))
|| ((ch >= 'A') && (ch <= 'Z'))
|| ((ch >= 'a') && (ch <= 'z'))) {
sb.append(ch);
} else {
encodeUrl(ch, sb);
}
}
return sb;
}
public static String escapeHTML(String str) {
if (str.length() == 0)
return str;
str = str.replaceAll("\r", "");
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
str = str.replaceAll("\n", "<br>");
str = str.replaceAll(" ", " ");
return str;
}
private static final void encodeUrl(int b, StringBuilder sb) {
sb.append('%');
sb.append(_HexDigits[(b >> 4 & 0xF)]);
sb.append(_HexDigits[(b & 0xF)]);
}
static final char[] _HexDigits = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
}