mysql中imagin的类型_Image转换成Mysql的blob类型 | 学步园

发现网上好多方法都不可行,下面是我的方法,略麻烦,纯属为自己以后的使用做个记录,如果你有更好的方法请留言,谢谢~

第二种方法,已知文件路径,通过构造文件的方式转换成blob很简单。如果很不幸,你只有Image,那就用第一种吧。。。

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import javax.imageio.ImageIO;

import javax.swing.*;

public class ImageIOTest {

Connection connection = null;

//连接到数据库

public void getConnection() {

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

System.out.println("装载驱动包出现异常!请查正!");

e.printStackTrace();

};

try {

// 数据库:bss 用户名group46 密码123456

connection = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/bss", "group46", "123456");

} catch (SQLException e) {

System.out.println("链接数据库发生异常!");

e.printStackTrace();

};

}

//释放连接

public void releaseConnection() {

try {

if (connection != null)

connection.close();

} catch (SQLException e) {

System.out.println(e.getMessage());

e.printStackTrace();

}

connection = null;

}

//将Image类型的对象转换成blob类型写入到数据库

public void writeToDatabaseWithImage() {

//假设你的程序中有image这样一个Image类型的对象

ImageIcon icon = new ImageIcon("f:\\11.jpg");

Image image = icon.getImage();

//首先将Image转换成BufferedImage

BufferedImage buf = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_3BYTE_BGR);

Graphics g = buf.createGraphics();

g.drawImage(image,0,0,null);

g.dispose();

//然后将BufferedImage写到ByteArrayOutputStream输出流中

ByteArrayOutputStream stream = new ByteArrayOutputStream();

try {

ImageIO.write(buf,"jpg",stream);

//-----------------------

//ImageIO.write(buf, "jpg", new FileOutputStream("f:\\12.jpg"));//此句可以将image写到另一个文件里

//-----------------------

}catch (Exception e) {

System.out.println("imageio error!");

}

//将ByteArrayOutputStream的内容写到ByteArrayInputStream,供sql执行语句写入

byte[] bytes = stream.toByteArray();

ByteArrayInputStream in = new ByteArrayInputStream(bytes);

try {

//此处以更改ISBN为1的图书条目的image为例,image为blob类型

String sql = "update book set image = ? where ISBN = '1'";

PreparedStatement statement = connection.prepareStatement(sql);

//此处将ByteArrayInputStream内容写入

statement.setBinaryStream(1, in, bytes.length);

statement.execute();

} catch (Exception e) {

System.out.println("sql error!");

}

}

//将文件类型的对象转换成blob类型写入到数据库

public void writeToDatabaseWithFile() {

try {

//此处以更改ISBN为1的图书条目的image为例,image为blob类型

String sql = "update book set image = ? where ISBN = '1'";

PreparedStatement statement = connection.prepareStatement(sql);

//从文件拿到FileInputStream

File image_file = new File("f:\\11.jpg");

FileInputStream fis = new FileInputStream(image_file);

statement.setBinaryStream(1, fis, (int) image_file.length());

}catch (Exception e) {

System.out.println("sql error!");

}

}

//下面进行测试

public static void main(String [] args) {

ImageIOTest test = new ImageIOTest();

test.getConnection();

test.writeToDatabaseWithImage();

test.releaseConnection();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值