我正在尝试加载MySQL数据库中的blob数据格式([B@e96bf)的图像,但无法将其加载到JLabel中,如下所示。它显示ByteArrayInputStream为空。
byte[] bytesl = null;
ResultSet rs = DB.DB.search("select image from imageio where id = '2'");
while (rs.next()) {
bytesl = rs.getBytes(1);
}
BufferedImage imag = ImageIO.read(new ByteArrayInputStream(bytesl));
Image img = imag;
img = img.getScaledInstance(jLabel1.getWidth(), jLabel1.getHeight(),
Image.SCALE_SMOOTH);
jLabel2.setIcon(new ImageIcon(img));发布于 2015-05-28 02:56:10
我认为您的代码不会转到while循环,因为没有包含此类查询的记录。最可能的问题是2周围的符号。通常id是一个数字,但在你的请求中,它看起来像是你将它与字符串进行了比较。尝试删除2周围的撇号符号。
发布于 2015-05-28 04:15:21
fileContent = Files.readAllBytes(f2.toPath());
DB.DB.statement("insert into imageio (image) values ('" + fileContent + "')");您已经将字符串[B@96bf插入到数据库中。ImageIO无法将其转换为图像,因此它返回null。
您至少应该使用PreparedStatement并将数据作为参数提供。你真的应该在这里使用Blob和输入流。
https://stackoverflow.com/questions/30487847
复制相似问题