springboot图片转base64,base64转图片

1、方法

package org.jeecg.utils;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Base64;

@Slf4j
public class Base64ImageUtils {

    /**
     * 图片转Base64字符串
     *
     * @param imageFileName
     * @return
     */
    public static String convertImageToBase64Str(String imageFileName) {
        ByteArrayOutputStream baos = null;
        try {
            //获取图片类型
            String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
            //构建文件
            File imageFile = new File(imageFileName);
            //通过ImageIO把文件读取成BufferedImage对象
            BufferedImage bufferedImage = ImageIO.read(imageFile);
            //构建字节数组输出流
            baos = new ByteArrayOutputStream();
            //写入流
            ImageIO.write(bufferedImage, suffix, baos);
            //通过字节数组流获取字节数组
            byte[] bytes = baos.toByteArray();
            //获取JDK8里的编码器Base64.Encoder转为base64字符
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    /**
     * Base64字符串转图片
     *
     * @param base64String
     * @param imageFileName
     */
    public static String convertBase64StrToImage(String base64String, String imageFileName) {
        ByteArrayInputStream bais = null;
        try {
            StringBuilder licensePlatePictureUrl = new StringBuilder();
            // 获取当前日期,格式为 "yyyyMMdd",用于构建文件夹名称
            LocalDate now = LocalDate.now();
            String dateFolder = now.toString().replace("-", "");
            String dateFile = imageFileName + dateFolder;

            // 创建以日期命名的文件夹,如果不存在的话
            File folder = new File(dateFile);
            if (!folder.exists()) {
                folder.mkdirs();
            }

            licensePlatePictureUrl.append(dateFile);
            licensePlatePictureUrl.append("/");
            licensePlatePictureUrl.append(Instant.now().getEpochSecond());
            licensePlatePictureUrl.append(".jpg");
            imageFileName = licensePlatePictureUrl.toString();
            //获取图片类型
            String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
            //获取JDK8里的解码器Base64.Decoder,将base64字符串转为字节数组
            byte[] bytes = Base64.getDecoder().decode(base64String);
            //构建字节数组输入流
            bais = new ByteArrayInputStream(bytes);
            //通过ImageIO把字节数组输入流转为BufferedImage
            BufferedImage bufferedImage = ImageIO.read(bais);
            //构建文件
            File imageFile = new File(imageFileName);
            //写入生成文件
            ImageIO.write(bufferedImage, suffix, imageFile);
            return imageFileName;
        } catch (Exception e) {
            log.error(e.getMessage(), e);

        } finally {
            try {
                if (bais != null) {
                    bais.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }


}

2、调用

String str = Base64ImageUtils.convertImageToBase64Str("E:\\1.jpg");
String imgUrl = Base64ImageUtils.convertBase64StrToImage(base64String, imageFileName); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值