package com.nantian.cloud.jljc.vo.util;

import java.io.*;
import java.util.Base64;

/**
 * 图片文件转为base64
 */
public class ImageToBase64Util {

    public static String convertFileToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            System.err.println("文件大小（字节）=" + in.available());
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组进行Base64编码，得到Base64编码的字符串
        // BASE64Encoder encoder = new BASE64Encoder();
        // String base64Str = encoder.encode(data);
        String base64Str = Base64.getEncoder().encodeToString(data);
        System.err.println("图片的base64编码：" + "data:image/png;base64," + base64Str);
        return base64Str;
    }

}
