I have read clob from oracle procedure and convert them into java.awt.Image object by the following code .
InputStream stream = clob.getAsciiStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
int a1 = stream.read();
while (a1 >= 0) {
output.write((char) a1);
a1 = stream.read();
}
Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
output.close();
Now, I want to save myImage to hard disk. So, What must I do to save these java.awt.Image to file ?
I have cast the myImage to BufferedImage and then write this to ImageIo by the following code :
BufferedImage bi = (BufferedImage)myImage;
ImageIO.write(bi, "jpg",new File("E:\\out.jpg"));
But I am getting the following exception :
Got Exception as : sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
at com.connect.home.Home.getHomeParameter(Home.java:103)
at com.connect.home.Home.main(Home.java:141)
How can I remove this error ? Any advice is of great help .
First
Oracle JDBC drivers support the manipulation of data streams in either direction between server and client. The drivers support all stream conversions: binary, ASCII, and Unicode. Following is a brief description of each type of stream:
Binary. Used for RAW bytes of data, and corresponds to the getBinaryStream method
ASCII. Used for ASCII bytes in ISO-Latin-1 encoding, and corresponds to the getAsciiStream method
Unicode. Used for Unicode bytes with the UTF-16 encoding, and corresponds to the getUnicodeStream method
So probably you will need to use clob.getBinaryStream
to retrieve your stream.
Second. You can not save ToolKitImage
whith ImageIO::write because it expect a RenderedImage
instance.And ToolkitImage
not implements this interface.
So you have two options.
Read the stream with ImageIO
RenderedImage image = ImageIO.read(clob.getBinaryStream());
ImageIO.write(image, "JPG",new File("E:\\out.jpg"));
Or draw the ToolkitImage
on a RenderedImage
BufferedImage bi = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(myImage,0,0, null);
ImageIO.write(bi, "JPG", new File("E:\\out.jpg"));
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.