简体   繁体   中英

How to convert CLOB to image or vice versa?

In Oracle 11g I have a table that has a CLOB column containing image data for each row. I need to convert the CLOB field back to an image. I searched a bit through google but I could not find a working example. Can anyone help please?

Thank you

I found the solution. This is what I used

public void convertFromClob(Clob c, File f2) {
    try {
        InputStream inStream = c.getAsciiStream();
        StringWriter sw = new StringWriter();
        IOUtils.copy(inStream, sw);

        // Transfer the data
        byte[] data = Base64.decodeBase64(sw.toString());
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(data));
        ImageIO.write(image, "png", f2);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

You can call directly the above method passing as parameters your Clob variable and the File to store the image. Tung

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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