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

查看:96
本文介绍了转换图像用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 和<一个href=\"http://commons.apache.org/proper/commons-$c$cc/apidocs/org/apache/commons/$c$cc/binary/Base64.html\"相对=nofollow> 的Base64

Using Apache IOUtils and Base64:

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

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

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