base64编码 base64解码 Clob转化成String
上传一个文件,将文件存储到数据库中,数据库Clob 类型,需要将数据先编码成 BASE64Encoder ,下载的时候 要解码 base64Decode
从数据库取出的Clob 不能使用toString(),需要用方法,否则数据会变多。
/**
* Base64编码.
*/
public static String base64Encode(byte[] input) {
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(input);
}
/**
* Base64解码.
*/
public static byte[] base64Decode(String input) {
BASE64Decoder decoder = new BASE64Decoder();
try {
return decoder.decodeBuffer(input);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 数据库Clob对象转换为String
*/
public static String clobToString(Clob clob) {
try {
Reader inStreamDoc = clob.getCharacterStream();
char[] tempDoc = new char[(int) clob.length()];
inStreamDoc.read(tempDoc);
inStreamDoc.close();
return new String(tempDoc);
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException es) {
es.printStackTrace();
}
return null;
}