报警弹窗改进:base64与图片互转工具类,图片保存到本地,数据库存放图片URL

之前获取到报警信息,将base64的编码用text类型直接存在了数据库里,发现一个问题;当图片过大的时候,没有成功弹窗,数据库没有保存到这条报警信息,后来查到是由于字符串超长,无法存入数据库,现在打算将收到的报警信息的base64先转成图片,保存到本地,再将图片的url存到数据库。

在找到一个base64的转换工具类以后,开始测试将base64转成图片:

public class test {
    public static void main(String[] args) throws IOException {
       Base64Utils.GenerateImage("", "D:/photos/企鹅.jpg");

    }
}

在这里插入图片描述

imgData是base64编码,后面是将它存到指定路径,命名为企鹅.jpg;
在这里插入图片描述
将编码复制到第一个参数里,运行报错:
在这里插入图片描述
这里我犯了一个错误,将编码直接定义成字面量字符串,就是用双引号括起来的,常量池中对String类型的结构体定义最多为65535字节。所以79kb的字符串就会报错。

这个方法应该是可以用的,就不再继续测试,直接用到我的方法里:
第一步:添加工具类Base64Utils:

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

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

    /**
     * ``
     * base64字符串转化成图片
     *
     * @param imgData     图片编码
     * @param imgFilePath 存放到本地路径
     * @throws IOException
     */
    @SuppressWarnings("finally")
    public static boolean generateImage(String imgData, String imgFilePath) throws IOException { // 对字节数组字符串进行Base64解码并生成图片
        if (null == imgData || "".equals(imgData)) { // 图像数据为空
            return false;
        }
        if (imgData.indexOf("data:image/jpeg;base64,") != -1) {
            imgData = imgData.replace("data:image/jpeg;base64,", "");
        }
        if (imgData.indexOf("data:image/png;base64,") != -1) {
            imgData = imgData.replace("data:image/png;base64,", "");
        }
        if (imgData.indexOf("data:audio/mp3;base64,") != -1) {
            imgData = imgData.replace("data:audio/mp3;base64,", "");
        }
        if (imgData.indexOf("data:video/mp4;base64,") != -1) {
            imgData = imgData.replace("data:video/mp4;base64,", "");
        }
        BASE64Decoder decoder = new BASE64Decoder();
        FileOutputStream 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);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
            return true;
        }
    }
}

两个方法实现了base64和图片的互转

第二步:应用在接口里:
在这里插入图片描述
我没写过图片保存之类的工具类,这里粗略的实现了一下,首先获取当前时间戳,然后把图片以报警信息code+时间戳命名,放在D盘photos文件夹中,以jpg结尾,调用Base64Utils的generateImage方法,第一个参数是编码,第二个参数是图片路径名称,然后数据库存放这个路径,其实这样有点硬编码的方式,不是很好,但是目前先不改进,后面需要再改动

,特意找了个4M大小的图片测试:
在这里插入图片描述
在这里插入图片描述
成功弹出,并且存放数据库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值