我正在尝试从 Java swing 应用程序将图像保存在 MySQL 数据库中。我正在使用 JFileChsoser 来获取图像的路径。然后转换文件,使其可以保存在 BLOB 类型的 MySQL 列中。但是我尝试保存的每张图像都无法正确保存或正确转换。有人可以告诉我我在这里做错了什么吗?
private void btn_choosepicActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser picchooser = new JFileChooser();
    picchooser.setDialogTitle("Select Image");
    picchooser.showOpenDialog(null);
    File pic=picchooser.getSelectedFile();
    path= pic.getAbsolutePath();
    txt_path.setText(path.replace('\\','/'));
    try{
        File image = new File(path);
        FileInputStream fis = new FileInputStream(image);
        ByteArrayOutputStream baos= new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        for(int readNum; (readNum=fis.read(buff)) !=-1 ; ){
            baos.write(buff,0,readNum);
        }
        userimage=baos.toByteArray();
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }     
}
然后在此之后我像这样将它保存到数据库中。
private void btn_saveActionPerformed(java.awt.event.ActionEvent evt) {
    String user= txt_username.getText();
    try{
        String sql="insert into imgtst (username,image) values ('"+user+"','"+userimage+"')";
        pst=con.prepareStatement(sql);
        pst.executeUpdate();
        JOptionPane.showMessageDialog(null, "Saved");
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }  
}
我已经将变量 userimage 和 path 声明为全局变量
String path=null;
byte[] userimage=null;