发现网上好多方法都不可行,下面是我的方法,略麻烦,纯属为自己以后的使用做个记录,如果你有更好的方法请留言,谢谢~
第二种方法,已知文件路径,通过构造文件的方式转换成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();
}
}