java使用hutool把base64编码转为图片
时间: 2023-06-22 16:47:20 浏览: 1276
使用Hutool将Base64编码转换为图片的代码如下:
```java
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
public class Base64ToImage {
public static void main(String[] args) {
// Base64编码字符串
String base64Str = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAMAUExURQAAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAKwAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAJgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAIwAAJgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAHgAAIwAAJgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAHAAAHgAAIwAAJgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAHAAAFAAAHgAAIwAAJgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAHAAAAAAAFwAAHgAAIwAAJgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAMgAAHAAAAAAAFwAAHgAAHAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFAAAFwAA";
// 去掉Base64编码字符串中的前缀
String base64 = base64Str.substring(base64Str.indexOf(",") + 1);
// 将Base64编码字符串解码为字节数组
byte[] bytes = Base64.decode(base64);
// 将字节数组写入图片文件
FileUtil.writeBytes(bytes, "test.png");
}
}
```
其中,使用`Base64.decode()`方法将Base64编码字符串解码为字节数组,然后使用`FileUtil.writeBytes()`方法将字节数组写入图片文件。
阅读全文