【问题标题】:convert base64 Image to Sql Blob in JAVA在 JAVA 中将 base64 图像转换为 Sql Blob
【发布时间】:2015-08-04 05:01:30
【问题描述】:

我正在尝试将 Base64 转换为 JAVA 中的 Blob,到目前为止,我已经开发了这段代码并且它正在工作,但它不是 Blob:

public static BufferedImage decodeToImage(String imageString) {

BufferedImage image = null;
byte[] imageByte;
try {
    BASE64Decoder decoder = new BASE64Decoder();
    imageByte = decoder.decodeBuffer(imageString);
    ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
    image = ImageIO.read(bis);
    bis.close();
} catch (Exception e) {
    e.printStackTrace();
}
return image;

}

我想我必须将 bufferedImage 转换为 byteArray ?我怎样才能改进我的代码?如果我什至不接近,我怎么能做对?

【问题讨论】:

  • 如果你已经有字节就不需要读取图像了...

标签: java base64 bytearray blob


【解决方案1】:

也许您需要一个SerialBlob?像这样:

import javax.sql.rowset.serial.SerialBlob;
.
.
.

public static SerialBlob decodeToImage(String imageString) {
    BASE64Decoder decoder = new BASE64Decoder();
    byte[] imageByte = decoder.decodeBuffer(imageString);    
    return new SerialBlob(imageByte);    
}

【讨论】:

  • 我将此作为返回的 blob javax.sql.rowset.serial.SerialBlob@5e1ac263 的输出我正在寻找这样的数据 FFFFFFFF FFFFFFD8 FFFFFFFF FFFFFFE0 0 10 4A 46 ... 如何我可以得到二进制数据吗?
【解决方案2】:

这是我的问题的有效解决方案:

我必须将我的 Base64 String 图像转换为 byte[]

public static byte[] Base64ToBytes(String imageString) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(imageString);
return decodedBytes;
}

希望这对其他人有帮助。

【讨论】:

  • 你问过如何转换为Sql Blob,你自己回答了如何转换为byte[]。你问错问题了。
  • 我会向你解释我的问题是什么,我可能解释错了,但我使用的是 eclipse Birt 报告,我有一个 JSON 对象,其中包含我需要转换为 Blob 的 Base64 编码图像反对把它放在我的report.rptdesign中,我要求base64到Blob,因为在birt中列图像类型是Blob,我尝试了你的解决方案但它没有用,所以我认为它可能需要字节并且它有效,无论如何我'我只与可能对某人有帮助的人分享此内容
  • 您的问题应该看起来像您的评论,更详细。
  • 无论如何,谢谢你的回答,它可能没有解决我的问题,但它帮助了我。
猜你喜欢
  • 2019-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 2022-08-12
  • 2017-07-11
  • 1970-01-01
  • 2013-09-10
相关资源
最近更新 更多