Convert image to BLOB

1,543 views
Skip to first unread message

pha...@payon.com.kh

unread,
Jan 28, 2022, 5:10:04 AM1/28/22
to vert.x
Hi,
Can we insert image to Oracle BLOB column?
Thanks!

Catalin Roman

unread,
Jan 28, 2022, 8:18:36 AM1/28/22
to vert.x
Yes, we can.

Phannin Yeat

unread,
Jan 30, 2022, 4:17:20 AM1/30/22
to ve...@googlegroups.com
Thanks for your answer!!
 
JsonArray can't add BLOB value. Could you help me?

try {
        File file = new File("00002192.jpg");

        FileInputStream input = new FileInputStream(file);
        byte[] bytes = new byte[(int) file.length()];
        input.read(bytes);

        Blob blob = new SerialBlob(bytes);
         JsonArray params = new JsonArray();

         params.add("0").add("").add(blob.getBinaryStream());

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

Error Message
image.png


--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/4347370f-b4e2-48e0-9a94-1255276fbe3cn%40googlegroups.com.

Catalin Roman

unread,
Feb 3, 2022, 10:36:12 AM2/3/22
to ve...@googlegroups.com
OK, your question is not related to vertx, but here is an answer.
About inserting an "image" into an Oracle BLOB column... this is possible, because BLOB is the perfect data type to store binary data such as images.
Regarding the JSON part... JSON is a text standard, and you can't really use it to store binary data. However, you can do that if you encode the binary data to some text format such as BASE64.
There are several ways to do that.  For instance Java 8 has its own encoder eg:
String encodedInput = Base64.getEncoder().encodeToString(originalInput.getBytes());
-Catalin

Phannin Yeat

unread,
Feb 3, 2022, 8:32:55 PM2/3/22
to ve...@googlegroups.com
Thank you so much.
I found how to insert images in a binary stream. 
We can use java.sql.Connnection by the above code.
Connection jdbcConn = sqlConnection.unwrap();
Blob blob = jdbcConn.createBlob();
blob.setBinaryStream(1).write(bytes);
JsonArray params = new JsonArray().add(blob);

connection.queryWithParams("INSERT INTO XXX (b) VALUES (?)", params, handler);

Reply all
Reply to author
Forward
0 new messages