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


基础教程推荐
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 从 python 访问 JVM 2022-01-01