5

I'm trying to store an Image in Postgresql Database, I have procedure which is used to store image in database, Image column type is bytea.I tried to convert my image into String(Base64) and converted it into the byte[], but not able to update that image.

Code to read Image And convert it into the String is This:-

 File file = new File("FilePath\\image.jpg");

 try
 {
     // Reading a Image file from file system
     FileInputStream imageInFile = new FileInputStream(file);
     byte imageData[] = new byte[(int) file.length()];
     imageInFile.read(imageData);

     // Converting Image byte array into Base64 String By calling encodeImage Function
     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 an image Link Given below is procedure which is used to save an image in database.

CREATE OR REPLACE FUNCTION UpdateProfileImage(product_id character varying, img bytea)

Can Anyone tell me why I'm not able to store this Image, or what I'm doing wrong..

Luffy
  • 1,317
  • 1
  • 19
  • 41
  • 1
    Why not read the image into a `ByteArrayOutputStream`, which will allow you to access the `byte[]` data...? – MadProgrammer Apr 23 '15 at 07:45
  • @MalProgrammer I also tried this but nothing happen :( – Luffy Apr 23 '15 at 07:47
  • Perhaps you should show you attempt as well as you stored procedure – MadProgrammer Apr 23 '15 at 07:50
  • @MadProgrammer I put some code here you can Check and I also Specify link which I used to convert an Image into String. – Luffy Apr 23 '15 at 07:58
  • What is the error you get? –  Apr 23 '15 at 08:01
  • @a_horse_with_no_name I'm Not getting any error perhaps it's not updating image into the Database. – Luffy Apr 23 '15 at 08:02
  • It's hard to give advice without seeing the definition of the stored procedure. In general AFAIK postgres does not accept base64 as a literal string format for bytea input, but maybe your stored procedure is converting it? – harmic Apr 23 '15 at 08:09
  • 1
    Use a `PreparedStatement` and `setBinaryStream()`, e.g: http://stackoverflow.com/a/8349906/330315 or http://stackoverflow.com/a/4339778/330315 –  Apr 23 '15 at 08:09
  • @a_horse_with_no_name Thanks for the suggestions I found the mistake that I'm Doing Thanks a lot.... – Luffy Apr 23 '15 at 08:55

1 Answers1

3

Thanks to a_horse_with_no_name. I'm able to found solution of my Problem. I don't need to call procedure to store Image I need to pass image as Binary Stream.

PreparedStatement pstmt = con.prepareStatement("UPDATE PRODUCTS SET IMAGE = ? WHERE ID = ?");
File file = new File("C:\\Program Files (x86)\\openbravopos-2.30.2\\image.jpg");
FileInputStream in = new FileInputStream(file);
try
{
    pstmt.setBinaryStream(1, in, (int) file.length());
    pstmt.setString(2, id);
    pstmt.executeUpdate();
    //con.commit
}
catch (Exception ee)
{
    System.out.println("Exception is:- " + ee);
}
Community
  • 1
  • 1
Luffy
  • 1,317
  • 1
  • 19
  • 41