Java BLOB到图像文件 [英] Java BLOB to image file

查看:108
本文介绍了Java BLOB到图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BLOB,它是从MySQL数据库中检索的,如下所示:

I have a BLOB that is retrieved from a MySQL database as follows:

Blob imageBlob;
while (rs.next()) {
    imageBlob= rs.getBlob("face");
}

之后,我的imageBlob是这样的:data:image/png;base64,iVBORw0KGgoAAAANSUhE................

after that my imageBlob is something like this: data:image/png;base64,iVBORw0KGgoAAAANSUhE................

我一直在四处搜寻,但是还没有找到解决我的问题的任何方法:如何创建图像文件并将其从此BLOB保存到磁盘上?

I've been googling around but I haven't found any solution to my problem: how do I create an image file and save it on the disk from this BLOB?

推荐答案

imageBlob存储图像数据的base64表示形式.要将其存储到磁盘上,您需要将该base64表示解码为原始的二进制格式表示.

imageBlob is storing the base64 representation of your image data. For storing that onto your disk you need to decode that base64 representation into the original binary format representation.

// Imports required
import java.util.Base64
import java.io.ByteArrayInputStream;
import java.io.File;
import javax.imageio.ImageIO;

String imageData = "data:image/png;base64,iVBORw0KGgoAAAANSUhE....";
String base64Data = imageData.split(",")[1]

byte[] decodedBytes = Base64.getDecoder().decode(base64Data);
ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes);
BufferedImage image = ImageIO.read(bis);

File outputFile = new File("output.png");
ImageIO.write(image, "png", outputFile);    

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

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