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

2023-04-24  本文已影响0人  不知不怪

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 这种方式能不用就不用

上一篇 下一篇

猜你喜欢

热点阅读