首页 文章

图像没有用java存储在数据库中

提问于
浏览
0

我试图使用BLOB数据类型将图像存储在数据库中,但是当我调用类来插入它时,它没有按预期执行,这很奇怪,因为它没有给我一个错误,我在方法后面放了一个“out.println()”打电话来看看它是否进入 .

这是插入的代码:

public void insertProfPic(int user_id, String file) {
        Connection conn = DatabaseConnection.getCon();
        FileInputStream imageInputStream = null;

        PreparedStatement prep = null;
        try{
            String query = "INSERT INTO images(user_id, images) VALUES (?,?)";
            prep = conn.prepareStatement(query);
            File fil = new File(file);
            imageInputStream = new FileInputStream(fil);

            prep.setInt(1, user_id);
            prep.setBinaryStream(2, imageInputStream,(int)file.length());

            prep.execute();
        }catch(FileNotFoundException | SQLException e){
            System.out.println(e);
        }finally {
                        // close resources 

            if(imageInputStream!=null){
                            try {
                                imageInputStream.close();
                            } catch (IOException ex) {
                                Logger.getLogger(imageUpload.class.getName()).log(Level.SEVERE, null, ex);
                            }
            }
        }
    }

这是我的方法调用:

String clicked = request.getParameter("submit");
    imageUpload user = new imageUpload();
    out.println(clicked);

    String img = request.getParameter("picture");
    user.insertProfPic(id, img);
    out.println("file path: " + img);

1 回答

  • 1

    尝试将图像编码为byte []并将其存储为二进制数据

相关问题