使用javafx将图像上传到数据库 [英] Uploading an image to a database with javafx

查看:152
本文介绍了使用javafx将图像上传到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用phpmyadmin将图像上传到mysql数据库,尽管该程序可以正常工作并存储其余数据,但是图像未正确存储.

i'm trying to upload an image to mysql database using phpmyadmin and despite the program works and stores the rest of the data the image is not stored correctly.

如果我直接在phpmyadmin中上传图像,其图像的大小为Blob类型,则为26.6 KB,但是如果我尝试使用javafx上传图像的大小,则约为10 B,所以我认为我没有正确上传.

If I upload an image directly in phpmyadmin the image's size in type Blob it's 26.6 KB but if I try to uploading using javafx the image's size it's about 10 B so I think i'm not uploading correctly.

@Override
public void guardarMonstruo(MonstruoDTO monstruo) {
    con= ConexionBDA.getInstance().getCon();
    try {
    if (con != null){
        byte[] blob = imagenToByte(monstruo.getImagen());
        System.out.println(blob.toString());
        statement = con.createStatement();
        statement.executeUpdate("INSERT INTO monstruos (Nombre,Habitat,Estado,ColaCercenable,DragonAnciano,DebilidadFuego,DebilidadAgua,Debilidadrayo,DebilidadHielo,DebilidadDraco,ImagenMonstruo) VALUES ('"+monstruo.getNombre()+"'"+","+"'"+monstruo.getHabitat()+"'"+","+"'"+monstruo.getEstado()+"'"+","+"'"+monstruo.getColaCercenable()+"'"+","+"'"+monstruo.getDragonAnciano()+"'"+","+"'"+monstruo.getDebilidadFuego()+"'"+","+"'"+monstruo.getDebilidadAgua()+"'"+","+"'"+monstruo.getDebilidadRayo()+"'"+","+"'"+monstruo.getDebilidadHielo()+"'"+","+"'"+monstruo.getDebilidadDraco()+"'"+","+"'"+blob+"');");
        con.close();
        statement.close();


    }
    }
    catch(Exception e) {
        e.printStackTrace();
    }


}

然后使用imagenToByte()方法将图像传递到字节是这样的:

And the method imagenToByte() is used to pass the image to byte is this:

private byte[] imagenToByte(Image imagen) {
    BufferedImage bufferimage = SwingFXUtils.fromFXImage(imagen, null);
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      try {
        ImageIO.write(bufferimage, "jpg", output );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      byte [] data = output.toByteArray();
      return data;
}

我不知道我在做什么错,请您能帮我吗?

I don't know what i'm doing wrong, could you please help me?

推荐答案

您将toString方法的结果插入查询字符串中,这将不会导致预期的结果.使用PreparedStatement并设置一个Blob参数:

You insert the result of the toString method to the query string which won't result in the desired outcome. Use PreparedStatement and set a Blob parameter instead:

try (PreparedStatement ps = con.prepareStatement("INSERT INTO monstruos (Nombre,Habitat,Estado,ColaCercenable,DragonAnciano,DebilidadFuego,DebilidadAgua,Debilidadrayo,DebilidadHielo,DebilidadDraco,ImagenMonstruo) VALUES (?,?,?,?,?,?,?,?,?,?,?)")) {
    ps.setString(1, monstruo.getNombre());
    ps.setString(2, monstruo.getHabitat());
    ps.setString(3, monstruo.getEstado());
    ps.setString(4, monstruo.getColaCercenable());
    ps.setString(5, monstruo.getDragonAnciano());
    ps.setString(6, monstruo.getDebilidadFuego());
    ps.setString(7, monstruo.getDebilidadAgua());
    ps.setString(8, monstruo.getDebilidadRayo());
    ps.setString(9, monstruo.getDebilidadHielo());
    ps.setString(10, monstruo.getDebilidadDraco());

    // upload the data, not the toString result of the array
    ps.setBlob(11, new SerialBlob(blob));

    ps.executeUpdate();
}

这篇关于使用javafx将图像上传到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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