java - 在 postgresql 中存储图像

标签 java postgresql hex pgadmin

我正在尝试将图像存储在 Postgresql 数据库中,我有用于将图像存储在数据库中的过程,图像列类型是 bytea。我试图将我的图像转换为字符串(Base64)并将其转换为字节[],但无法更新该图像。

读取图像并将其转换为字符串的代码是这样的:-

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

我使用此链接转换图像 Link 下面给出的是用于将图像保存在数据库中的过程。

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

谁能告诉我为什么我不能存储这张图片,或者我做错了什么..

最佳答案

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

关于java - 在 postgresql 中存储图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29816821/

相关文章:

java - 扩展 JTable 中的键绑定(bind)

postgresql - Hibernate 和 PostgreSQL : REPEATABLE_READ and use of @Version annotation to avoid write skews and other phenomena

c++ - 十六进制编辑器如何如此快速地显示数据?

SQL 将数字转换为任何基数的字符串表示形式(二进制、十六进制、...、三十六进制)

python - 线程应用程序 + IntegrityError

C++如何将字符转换为十六进制

java - 如何使用bamboo部署独立的java程序

java - 如何在 Retrofit GET API 中传递查询参数?

java - Spring Security 中缺少 ROLE_ANONYMOUS

sql - 我如何优化这个 JOIN 查询?