Java Swing中读取/写入图片到MySQL中类型转换

来源:互联网 发布:闪电侠剧情介绍知乎 编辑:程序博客网 时间:2024/06/09 16:26

图片等二进制数据文件写入到数据库
1、获取到 字节数组

File headerFile = new File(FileName);
FileInputStream inputStream = newFileInputStream(headerFile);
byte[] imageData = new byte[inputStream.available()];
inputStream.read(imageData);
保存到数据库
2、Blob photo = new SerialBlob(imageData);

3、preparedStatement.setBlob(1,photo);


从数据库读取图片等二进制数据文件
1、Blob photo = resultSet.getBlob(1);

2、通过blob对象中的 getBytes方法 返回字节数组

byte[] imageData =photo.getBytes(1,photo.getBinaryStream().available());

3、ImageIcon构造函数 显示
ImageIcon imageIcon = new ImageIcon(imageData);
原创粉丝点击