使用java将图像转换为base64 [英] convert image to base64 with java

查看:32
本文介绍了使用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 IOUtilsBase64:

Using Apache IOUtils and Base64:

byte[] imageBytes = IOUtils.toByteArray(new URL("...")));
String base64 = Base64.getEncoder().encodeToString(imageBytes);

这篇关于使用java将图像转换为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆