Convert base64 image to local file

Original link: https://www.jylt.cc/#/detail?id=dda55435cfa02dfbfc91729e3d3daf08

background

When we save images every day, we will put the image locally on the server or upload it to other servers, and then put the path of the image into the database. This can not only reduce the space occupied by the database, but also reduce the operating pressure of the database.

However, rich text editors sometimes paste base64 images into the database. If this type of data is directly stored in the database, the space occupied by the database will increase dramatically, which is very unreasonable. At this time, our ideal state is to convert the base64 image into an image file and store it, and replace the base64 address with the actual address of the file.

Base64 to local file method


/**
 * base64图片转为本地文件
 *
 * @param baseStr base64图片地址
 * @return 图片文件
 */
private static File base64ToFile (String baseStr) {
    
    
        BASE64Decoder decoder = new BASE64Decoder();
        try {
    
    
            Base64ImgInfo base64ImgInfo = getBase64ImgInfo(baseStr);
            if (base64ImgInfo == null) {
    
    
                return null;
            }
            byte[] b = decoder.decodeBuffer(base64ImgInfo.getImgStr());
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
    
    
                if (b[i] < 0) {
    
    
                    b[i] += 256;
                }
            }
            return File.createTempFile(UUID.randomUUID().toString(), base64ImgInfo.getType());
        } catch (IOException e) {
    
    
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取base64图片信息
     *
     * @param baseStr base64字符串
     * @return base64图片信息
     */
    private static Base64ImgInfo getBase64ImgInfo(String baseStr) {
    
    
        //允许的图片格式(可配置)
        String imgType = "jpg,png,jpeg,gif";
        if (!StringUtils.isEmpty(imgType)) {
    
    
            String[] imgTypes = imgType.split(",");
            Pattern pattern;
            Matcher matcher;
            String regex;
            for (String v : imgTypes) {
    
    
                regex = MessageFormat.format("data:image/{0};base64,", v);
                pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
                matcher = pattern.matcher(baseStr);
                if (matcher.lookingAt()) {
    
    
                    Base64ImgInfo base64ImgInfo = new Base64ImgInfo();
                    base64ImgInfo.setImgStr(matcher.replaceFirst(""));
                    base64ImgInfo.setType("." + v);
                    return base64ImgInfo;
                }
            }
        }
        return null;
    }

    @Data
    public static class Base64ImgInfo {
    
    
        /**
         * 图片格式,比如.png
         */
        private String type;

        /**
         * base64正文部分,去除前面data:image/png;base64,标识
         */
        private String imgStr;
    }

base64ToFileIn this way, you can get the base64 converted image file by calling the method .

https://data-water.oss-cn-beijing.aliyuncs.com/%E5%85%AC%E4%BC%97%E5%8F%B7%E7%B4%A0%E6%9D%90/%E6%89%AB%E7%A0%81_%E6%90%9C%E7%B4%A2%E8%81%94%E5%90%88%E4%BC%A0%E6%92%AD%E6%A0%B7%E5%BC%8F-%E7%99%BD%E8%89%B2%E7%89%88.png

Guess you like

Origin blog.csdn.net/wuchenxiwalter/article/details/124281155