Luf*_*ffy 5 java postgresql hex pgadmin
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);
}
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么我不能存储这个图像,或者我做错了什么..
感谢a_horse_with_no_name。我能够找到我的问题的解决方案。我不需要调用过程来存储图像,我需要将图像作为二进制流传递。
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);
}
Run Code Online (Sandbox Code Playgroud)