数据库
首页 > 数据库> > 19.对图片进行base64编码,存入数据库

19.对图片进行base64编码,存入数据库

作者:互联网

1.工具类

package com.atguigu.yygh.common.util;

import org.apache.commons.codec.binary.Base64;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class ImageBase64Util {

    public static void main(String[] args) {
        String imageFile= "G:\\0722test\\111.png";// 待处理的图片
        System.out.println(getImageString(imageFile));
    }

    public static String getImageString(String imageFile){
        InputStream is = null;
        try {
            byte[] data = null;
            is = new FileInputStream(new File(imageFile));
            data = new byte[is.available()];
            is.read(data);
            return new String(Base64.encodeBase64(data));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                    is = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }
}

测试

<img src="data:image/png;base64,iVBORw0..>,改文件名后缀

 

 

 

标签:String,19,base64,存入,imageFile,new,import,null,data
来源: https://www.cnblogs.com/javakangkang/p/15098551.html