转换图像用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.

不过,我似乎无法弄清楚如何退出这个功能。是否有任何人谁拥有一张code为这个,我可以很容易地使用?

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);