2025年mysql中imagin的类型_Image转换成Mysql的blob类型 - 学步园

mysql中imagin的类型_Image转换成Mysql的blob类型 - 学步园发现网上好多方法都不可行 下面是我的方法 略麻烦 纯属为自己以后的使用做个记录 如果你有更好的方法请留言 谢谢 第二种方法 已知文件路径 通过构造文件的方式转换成 blob 很简单 如果很不幸 你只有 Image 那就用第一种吧 import java

大家好,我是讯享网,很高兴认识大家。

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

第二种方法,已知文件路径,通过构造文件的方式转换成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 密码

connection = DriverManager.getConnection(

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

} 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();

}

}

小讯
上一篇 2025-02-23 11:52
下一篇 2025-02-18 11:11

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/22200.html