base64Encode 与 base64Decode 应用

base64Encode 与 base64Decode 应用
/**
	 * Gets the encoding String of base64Encode by a String.
	 * 
	 * @param str
	 *            The source String.
	 * @return a new String that have been encoded.
	 */
public static String base64Encode(String str) {
	try {
		return base64Encode(str.getBytes("UTF-8"));
	} catch (Exception ex) {
		log.fatal(ex.toString());
	}
	return "";
}

/**
 * Gets the decoding String of base64Decode.
 * 
 * @param str
 *            The source String.
 * @return a new String that have been decoded.
 */
public static String base64Decode(String str) {
	return  base64Decode(str.getBytes());
	}

/**
 * Gets the encoding String of base64Encode by a byte array.
 * 
 * @param str
 *            The source byte array.
 * @return a new String that have been encoded.
 */
public static String base64Encode(byte[] str) {
	return new String((new Base64()).encode(str));
}

/**
 * Gets the decoding String of base64Decode by a byte array.
 * 
 * @param str
 *            The source byte array.
 * @return a new String that have been decoded.
 */
public static String base64Decode(byte[] str) {
	if (str != null && str.length > 0) {
		byte buf[] = (new Base64()).decode(str);
		try {
						return new String(buf, "UTF-8");
		} catch (Exception ex) {
			log.fatal(ex.toString());
		}
	}
	return "";
}

 

你可能感兴趣的:(base64Encode 与 base64Decode 应用)