base64转换为图片以及图片转换为base64码

public class base64Change {
    /**
     * @param imgStr base64编码字符串
     * @param path   图片路径-具体到文件
     */
    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
// 解密
            byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    //图片转化成base64字符串  
    public static String GetImageStr()  
    {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
        String imgFile = "F:\tupian\a.jpg";//待处理的图片
                         // 地址也有写成"F:/deskBG/86619-107.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();  
        }  
        //对字节数组Base64编码  
        BASE64Encoder encoder = new BASE64Encoder();  
        return encoder.encode(data);//返回Base64编码过的字节数组字符串  
    }

}
public String ChangeBase64(String base64data,Integer certifiedtype,Integer userid,HttpServletRequest request) {
    //这一步很重要很重要很重要,因为base64的数据会有data:base64img,
    //所有需要将这个截取掉之后转化,不然就是空白的打不开的文件
    String base64img = certifieddata.substring(base64data.indexOf(",")+1);

    FileUploadUtils.generateImage(base64img,"F:/deskBG/86619-107.jpg");
    //return null;是为了测试的,实际需求自己定
    return null;
  }
原文地址:https://www.cnblogs.com/a591378955/p/9564286.html