Java实现图片和Base64之间的相互转化

1 看一堆网上的代码 不想说啥了

package com.ldr.common.utils;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import lombok.SneakyThrows;

public class ImageUtil {

    /**
     * 图片转Base64字符串
     */
    @SneakyThrows
    public static String toString(String path) {
        return Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(path)));
    }

    /**
     * Base64字符串转图片
     */
    @SneakyThrows
    public static void toImage(String string, String path) {
        Files.write(Paths.get(path), Base64.getDecoder().decode(string));
    }

    public static void main(String[] args) {
        toImage(toString("d:/3.png"), "d:/4.png");
    }
}

2 这种方式能不用就不用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。