import sun.misc.BASE64Decoder;
/**
* 将Base64位编码的图片进行解码,并保存到指定目录
*
* @param base64 base64编码的图片信息
* @return
*/
public void decodeBase64ToImage(String base64, String path) {
BASE64Decoder decoder = new BASE64Decoder();
String baseValue = base64.replaceAll(" ", "+");//前台在用Ajax传base64值的时候会把base64中的+换成空格,所以需要替换回来。
try {
FileOutputStream write = new FileOutputStream(new File(path));
//去除base64中无用的部分"data:image/jpg;base64,",这里只是做一个jpg的例子,图片后缀视情况而定
byte[] decoderBytes = decoder.decodeBuffer(baseValue.replace("data:image/jpg;base64,", ""));
write.write(decoderBytes);
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}