简体   繁体   中英

Entering an image in mysql 8.0 results in a null value or only with 4 bytes

When I insert an image into my table in the mysql database in java, nothing unusual happens, like error or some exception, sign that everything went well. but when i go to mysql i see the field empty or containing 4 bytes only.am working with java 9 and mysql 8.0.15

If anyone has ever faced this kind of problem or knows how to help me, thank you in advance.

this is the code

try(PreparedStatement ps = Conexão.conn.prepareStatement("INSERT INTO 
usuario(avatar, `desc`) VALUES(?,?)")) {

  ps.setBinaryStream(1,ipstram,longitudeBites);
  ps.setString(2,"Desc"+ Time.from(Instant.now()));

  if (new Usuario().inserir(ps)) {
      jfxSnackbar.show("Usuario Inserido com exito","OK",5000,event1 -> 
      jfxSnackbar.close());
      ipstram=null;
            }
  else {
      jfxSnackbar.show("Inserção Mal Sucesida","OK",5000,event1 -> 
      jfxSnackbar.close());
      }

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



public class Usuario{

 public boolean inserir(PreparedStatement ps) throws SQLException {
        if (ps.executeUpdate() > 0) {
            return true;
        } else {
            return false;
        }
    }

From the question we cannot see what ipstram and longitudeBites contain.

In fact best would be, if you verified their validity yourself. Check this:

byte[] data = ipstram.readAllBytes();
logger.info("### Data length: {}, expected: {}", data.length, longitudeBites);
Files.write(Paths.get("/.../temp.jpg"), data);
ipstram = new ByteArrayInputStream(data);

ps.setBinaryStream(1, ipstram, longitudeBites);
ps.setString(2,"Desc"+ Time.from(Instant.now()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM