JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

Java之base64转化成图片文件

wys521 2024-11-25 10:58:43 精选教程 25 ℃ 0 评论


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;
        }
    }
}

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表