package com.ssh.insure.utils;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
/**
* base64格式转换
* @author JD 2018.12.19
*/
public class Base64Util {
private static final Logger log = LoggerFactory.getLogger(Base64Util.class);
public static String encode (String data , String charset) {
if(charset == null || charset.equals("")){
charset = "UTF-8";
}
try {
byte[] dataBytes = Base64.encodeBase64(data.getBytes(charset));
String dataBase64 = new String(dataBytes, charset);
if (dataBase64 != null) {
dataBase64 = dataBase64.trim();
}
return dataBase64;
} catch (UnsupportedEncodingException e) {
log.error("Base64Util.encode UnsupportedEncodingException", e);
return "";
}
}
public static String decode (String data , String charset) {
if(charset == null || charset.equals("")){
charset = "UTF-8" ;
}
try {
byte[] dataBytes = Base64.decodeBase64(data.getBytes(charset));
String dataStr = new String(dataBytes, charset);
return dataStr;
} catch (UnsupportedEncodingException e) {
log.error("Base64Util.decode UnsupportedEncodingException", e);
return "";
}
}
}