java - Storing an image in postgresql -
i'm trying store image in postgresql database, have procedure used store image in database, image column type bytea.i tried convert image string(base64) , converted byte[], not able update image.
code read image , convert string this:-
file file = new file("filepath\\image.jpg"); try { // reading image file file system fileinputstream imageinfile = new fileinputstream(file); byte imagedata[] = new byte[(int) file.length()]; imageinfile.read(imagedata); // converting image byte array base64 string 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 link convert image link given below procedure used save image in database.
create or replace function updateprofileimage(product_id character varying, img bytea)
can tell me why i'm not able store image, or i'm doing wrong..
thanks a_horse_with_no_name. i'm able found solution of problem. don't need call procedure store image need pass image binary stream.
preparedstatement pstmt = con.preparestatement("update products set image = ? 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); }