在postgresql中存储图像 [英] Storing an image in postgresql

查看:163
本文介绍了在postgresql中存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Postgresql数据库中存储图像,我有用于将图像存储在数据库中的过程,图像列类型是bytea.I试图将我的图像转换为String(Base64)并将其转换为字节

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)

任何人都可以告诉我为什么我无法存储此图像,或者我'm doing wrong ..

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

推荐答案

感谢 a_horse_with_no_name 。我能找到我的问题的解决方案。我不需要调用过程来存储图像我需要将图像传递为二进制流。

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);
}

这篇关于在postgresql中存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆