Java实现图片和Base64之间的相互转化
作者:
不知不怪 | 来源:发表于
2023-04-24 19:23 被阅读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 这种方式能不用就不用
本文标题:Java实现图片和Base64之间的相互转化
本文链接:https://www.haomeiwen.com/subject/owaujdtx.html
网友评论