- 原理及作用
http://www.cnblogs.com/chengxiaohui/articles/3951129.html - 将文件转换为base64编码字符串
public static String encodeImgToBase64(String path){
String result = null;
try {
FileInputStream in = new FileInputStream(path);
byte[] datas = new byte[in.available()];
in.read(datas);
result = Base64.encodeBase64String(datas);
System.out.println("result = "+result.length());
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
- 将base64编码的字符串写入本地文件
public static void decodeBase64ToImg(String source){
try {
FileOutputStream os = new FileOutputStream(targetPath);
byte[] datas = Base64.decodeBase64(source);
os.write(datas);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}