繁体   English   中英

在JAVA中将base64图像转换为Sql Blob

[英]convert base64 Image to Sql Blob in JAVA

我正在尝试将JAVA中的Base64转换为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吗? 如何改善我的代码? 如果我什至没有关闭,我该怎么办呢?

也许您需要一个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);    
}

这是我的问题的工作解决方案:

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

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

希望这会帮助别人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM