Java base64转图片

import sun.misc.BASE64Decoder;

import java.io.FileOutputStream;
import java.io.OutputStream;



public class Base64Img {

    //imgStr base64字符串   url 图片存放地址
    public static boolean GenerateImage(String imgStr,String url) {   //对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) //图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {//调整异常数据
                    b[i] += 256;
                }
            }
           OutputStream out = new FileOutputStream(url);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/lixxx/p/10719153.html