Search code examples
javaimageoracle11gclob

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


Solution

  • 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