当前位置:网站首页>将本地上的图片转换成Base64编码

将本地上的图片转换成Base64编码

2022-04-23 21:59:00 多汁多味

导入需要的包:

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

使用Java根据本地上保存图片的地址转为Base64编码 

    /**
     * 将图片转换成Base64编码
     * @param imgFile 待处理图片
     * @return
     */
    public static String ImgToBase64(String imgFile){
        //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        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();
        }
        return new String(Base64.getEncoder().encode(data));
    }

版权声明
本文为[多汁多味]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43652507/article/details/124370438