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
基础教程推荐
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
