Jfinal把获取的base64转换为图片

public void test() {
boolean boo = false;
String img = this.getPara("img",
"base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAACWCAYAAACb3McZAAAgAElEQVR4Xmy96a9l53Xm9+z5THeuW7dGFotTURwkS7Isp9twJ0EnATofAqT/gARIgAaCIB+CIED+gs6nTuw2YCR22x3L7sRyyzYRSzItU6JkSiItURMpzixWsaY7D2fc8xv81rtPkQYigqriHc7ZZ+/3XetZz/Os9QbVzcJNqlSVpCCQXCCFrRS18v8LnP0RukBtJLVB973WSWEgtVIYSVFqf1WaSW0pLSqnKAgUSGoaKUmkKJHqtlUSh/aazsm+b//nJNe9J9fA2/");// 接收经过base64编
int dex = img.indexOf("base64,");
img = img.substring(dex + 7);
String imgFilePath = PathKit.getWebRootPath() + "\\" + "avatar" + File.separator + "test111.png";// 新生成的图片
if (img == null) // 图像数据为空
boo = false;
try {
// 生成jpeg图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(Base64.decodeBase64(img));
out.flush();
out.close();
boo = true;
} catch (Exception e) {
boo = false;
throw new RuntimeException();
}
renderJson(boo);

}


   //图片和base64互相转换

       /**
 * 图片转化成base64字符串
 *
 */

public static String getImageStr() {                      // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "D:\\eclipse_workspace\\hui_sports\\webapp\\avatar"+"test.jpg";// 待处理的图片
InputStream in = null;
byte[] data = null;
try {                                                                                                                       // 读取字节数组
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
String  base=Base64.encodeBase64(data);                                                  // 对字节数组Base64编码
return new String(base);                                                                             // 返回Base64编码过的字节数组字符串
}


       /**
* base64字符串转化成图片
*
*/
public static boolean generateImage(String imgFilePath, String img) { // 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null)                                                                                // 图像数据是否为空
return false;
try {                                                                                                     // 生成图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(Base64.decodeBase64(img));
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}

猜你喜欢

转载自blog.csdn.net/m0_37934074/article/details/79715828