Java之base64轉化成圖片檔案

package com。biubiu。utils; import java。io。FileOutputStream;import java。io。OutputStream;import java。util。Base64; public class Utils { /** * base64轉化成圖片檔案 * @param base64 * @param imgFilePath * @return */ public static boolean generateImage(String base64, String imgFilePath) {// 對位元組陣列字串進行Base64解碼並生成圖片 if (base64 == null) // 影象資料為空 return false; try { // Base64解碼 byte[] bytes = Base64。getDecoder()。decode(base64); for (int i = 0; i < bytes。length; ++i) { if (bytes[i] < 0) {// 調整異常資料 bytes[i] += 256; } } // 生成jpeg圖片 OutputStream out = new FileOutputStream(imgFilePath); out。write(bytes); out。flush(); out。close(); return true; } catch (Exception e) { return false; } }}