> 文章列表 > base64转图片和原始图大小有出入,直接上代码

base64转图片和原始图大小有出入,直接上代码

base64转图片和原始图大小有出入,直接上代码

/* base64字符串转化成图片* @param imgData base64图片编码* @param imgFilePath 存放到本地路径* @return* @throws IOException*/
public static boolean generateImage(String imgData, String imgFilePath) throws IOException { // 对字节数组字符串进行Base64解码并生成图片if (imgData == null) {return false;}BASE64Decoder decoder = new BASE64Decoder();OutputStream out = null;try {out = new FileOutputStream(imgFilePath);// Base64解码byte[] b = decoder.decodeBuffer(imgData);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {b[i] += 256;}}out.write(b);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {out.flush();out.close();return true;}
}