如何用JSON表示数据库中的图像

编程入门 行业动态 更新时间:2024-10-11 09:23:45
本文介绍了如何用JSON表示数据库中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要根据数据库中的blob创建JSON。为了获得blob图像,我使用下面的代码并在json数组中显示之后:

I need to create JSON based on a blob from database. To get the blob image, I use the code below and after show in json array:

Statement s = connection.createStatement(); ResultSet r = s.executeQuery("select image from images"); while (r.next()) { JSONObject obj = new JSONObject(); obj.put("img", r.getBlob("image")); }

我希望根据图像blob为每个图像返回一个JSON对象。我怎样才能实现它?

I to want return a JSON object for the each image according the image blob. How can I achieve it?

推荐答案

JSON中的二进制数据通常最好用 Base64 - 编码表格。您可以使用提供的标准Java SE DatatypeConverter#printBase64Binary() 方法对Base64进行字节数组编码。

Binary data in JSON is usually best to be represented in a Base64-encoded form. You could use the standard Java SE provided DatatypeConverter#printBase64Binary() method to Base64-encode a byte array.

byte[] imageBytes = resultSet.getBytes("image"); String imageBase64 = DatatypeConverter.printBase64Binary(imageBytes); obj.put("img", imageBase64);

另一方只需对Base64进行解码即可。例如。在Android中,您可以使用内置的 android.util.Base64 此API。

The other side has just to Base64-decode it. E.g. in Android, you could use the builtin android.util.Base64 API for this.

byte[] imageBytes = Base64.decode(imageBase64, Base64.DEFAULT);

更多推荐

如何用JSON表示数据库中的图像

本文发布于:2023-11-30 19:26:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   如何用   图像   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!