Python 官方文档:入门教程 => 点击学习
由于jdk 1.8 之后sun.misc 包下的 BASE64Decode的依赖 被移除 我们需要在自己的项目中引入EncodeUtils 工具类 帮助我们进行转换 public
我们需要在自己的项目中引入EncodeUtils 工具类 帮助我们进行转换
public class EncodeUtils {
private static final String DEFAULT_URL_ENcoding = "UTF-8";
public static String base64Encode(byte[] input) {
return new String(Base64.encodeBase64(input));
}
public static byte[] base64Decode(String input) {
return Base64.decodeBase64(input);
}
}
直接调用工具类中的方法即可
static void base64StringToImage(String base64String) {
try {
byte[] bytes1 = EncodeUtils.base64Decode(base64String);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 = Imageio.read(bais);
File f1 = new File("F:/wpcache/test/test.jpg");
ImageIO.write(bi1, "jpg", f1);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getImgStr(String imgFile) {
// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return EncodeUtils.base64Encode(data);
}
在main方法中运行
public static void main(String[] args) {
String base64Str = getImgStr("F:/wpcache/2.jpg");
System.out.println(base64Str);
base64StringToImage(base64Str);
}
运行结果
图片转BASE64 效果图
BASE64 转图片 效果图
(注:这个生成的图片路径就是自己在base64StringToImage 方法中的路径)
到此这篇关于关于JAVA11中图片与BASE64相互转换的实现的文章就介绍到这了,更多相关JAVA11图片与BASE64相互转换内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 关于JAVA11中图片与BASE64相互转换的实现
本文链接: https://www.lsjlt.com/news/124137.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0