convert image to base64 with java(使用java将图像转换为base64)
问题描述
我需要将图像对象转换为 base64 对象,以便将其加载到客户端的标签中.
I need to convert a image object to a base64 object so I can load it into the tag on my client side.
但是我似乎无法弄清楚如何解决这个问题.有没有人有一段我可以轻松使用的代码?
However I can't seem to figure out how to pull this off. Is there anyone who has a piece of code for this that I could use easily?
这就是我用来把外部图片链接变成图片对象的方法
This is what I use to turn the external image link into a image object
Image image = null;
URL url = new URL(request.getParameter("hdn_path"));
image = ImageIO.read(url);
不确定我是否以正确的方式进行.
not sure if I'm going about this the correct way.
推荐答案
使用 Apache IOUtils
和 Base64
:
Using Apache IOUtils
and Base64
:
byte[] imageBytes = IOUtils.toByteArray(new URL("...")));
String base64 = Base64.getEncoder().encodeToString(imageBytes);
这篇关于使用java将图像转换为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用java将图像转换为base64


- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01