java上传头像照片,java和base64格式相互转化

今天要做一个功能,就是要将学生的头像批量导入,客户确定的说,没涨头像不超过100kb大小,那么我就像直接把头像写入到数据库的学生表里去,平时不查询它即可。

oracle的数据库字段:

字段名:zp

字段类型:blob

java接收的实体类:

private byte[] zp;//照片

jsp页面展示base64格式的图片:

<img  src="https://img-blog.csdnimg.cn/2022010615495269625.png"color:#3399ea;">${zp}" alt="">

使用java-base64转换图片:

直接用即可,里面有给定源文件路径来想换转化base64;

给定 MultipartFile file 类型的文件数据来转化成base64;

package com.inco.project.dzxt.gly.kshwh.util_Base64;

import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Base64Utils {
    /**
     * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
     *
     * @param imgPath
     */
    public static String GetImageStr(String imgPath) {
        String imgFile = imgPath;// 待处理的图片
        InputStream in = null;
        byte[] data = null;
        String encode = null; // 返回Base64编码过的字节数组字符串
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        try {
            // 读取图片字节数组
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            encode = encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return encode;
    }

    /**
     * 字节数组字符串进行Base64解码并生成图片
     *
     * @param imgData     图片编码
     * @param imgFilePath 存放到本地路径
     */
    public static boolean GenerateImage(String imgData, String imgFilePath) throws IOException {
        if (imgData == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        OutputStream out = null;
        try {
            out = new FileOutputStream(imgFilePath);
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgData);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            out.write(b);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
            return true;
        }
    }

    /**
     * 将 MultipartFile file 类型的数据图片转换成base64格式的数据
     * @param file
     * @return
     * @throws Exception
     */
    public static String uploadiong(MultipartFile file) throws Exception{
        boolean image = isImage(file.getInputStream());
        if (image) {
            BASE64Encoder encoder = new BASE64Encoder();
            String imageString = "data:image/jpg;base64," + encoder.encode(file.getBytes());
            return imageString;
        }else {
            return null;
        }
    }

    public static boolean isImage(InputStream inputStream) {
        if (inputStream == null) {
            return false;
        }
        Image img;
        try {
            img = ImageIO.read(inputStream);
            return !(img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0);
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) throws IOException {
        String imageStr = Base64Utils.GetImageStr("D://平传胜.jpg");
        System.out.println(imageStr);
        Base64Utils.GenerateImage(imageStr, "D://平传胜-base64转成jpg.jpg");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值