I am trying to save an image in a Postgresql database, I have a procedure that is used to store an image in a database, the image column type is bytea.I tried to convert my image to String (Base64) and convert it to byte [], but failed refresh this image.
Code for reading an image and converting it to a string: This: -
File file = new File("FilePath\\image.jpg");
try
{
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
byte[] imageDataString = encodeImage(imageData);
CallableStatement statement = con.prepareCall(" { call products_update_image( '" + id + "', '" + imageDataString + "') } ");
statement.execute();
}
catch (Exception ex)
{
System.out.println("Exception is:- " + ex);
}
public static byte[] encodeImage(byte[] imageByteArray)
{
return Base64.encodeBase64(imageByteArray);
}
I used this link to convert the image. Link
The following is the procedure that is used to save the image in the database.
CREATE OR REPLACE FUNCTION UpdateProfileImage(product_id character varying, img bytea)
Can someone tell me why I cannot save this image or what I am doing wrong.